mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-27 10:47:06 +02:00
preparing for v0.5
This commit is contained in:
50
src/proxy/provider/file_provider.go
Normal file
50
src/proxy/provider/file_provider.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/yusing/go-proxy/common"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
M "github.com/yusing/go-proxy/models"
|
||||
U "github.com/yusing/go-proxy/utils"
|
||||
W "github.com/yusing/go-proxy/watcher"
|
||||
)
|
||||
|
||||
type FileProvider struct {
|
||||
fileName string
|
||||
path string
|
||||
}
|
||||
|
||||
func FileProviderImpl(m *M.ProxyProvider) ProviderImpl {
|
||||
return &FileProvider{
|
||||
fileName: m.Value,
|
||||
path: path.Join(common.ConfigBasePath, m.Value),
|
||||
}
|
||||
}
|
||||
|
||||
func Validate(data []byte) E.NestedError {
|
||||
return U.ValidateYaml(U.GetSchema(common.ProvidersSchemaPath), data)
|
||||
}
|
||||
|
||||
func (p *FileProvider) GetProxyEntries() (M.ProxyEntries, E.NestedError) {
|
||||
entries := M.NewProxyEntries()
|
||||
data, err := E.Check(os.ReadFile(p.path))
|
||||
if err.IsNotNil() {
|
||||
return entries, E.Failure("read file").Subject(p.fileName).With(err)
|
||||
}
|
||||
ne := E.Failure("validation").Subject(p.fileName)
|
||||
if !common.NoSchemaValidation {
|
||||
if err = Validate(data); err.IsNotNil() {
|
||||
return entries, ne.With(err)
|
||||
}
|
||||
}
|
||||
if err = entries.UnmarshalFromYAML(data); err.IsNotNil() {
|
||||
return entries, ne.With(err)
|
||||
}
|
||||
return entries, E.Nil()
|
||||
}
|
||||
|
||||
func (p *FileProvider) NewWatcher() W.Watcher {
|
||||
return W.NewFileWatcher(p.fileName)
|
||||
}
|
||||
Reference in New Issue
Block a user