replace all schema check with go-playground/validator/v10

This commit is contained in:
yusing
2024-12-18 04:48:29 +08:00
parent 00f60a6e78
commit 6aefe4d5d9
23 changed files with 149 additions and 250 deletions

View File

@@ -16,11 +16,10 @@ import (
"github.com/yusing/go-proxy/internal/notif"
proxy "github.com/yusing/go-proxy/internal/route/provider"
"github.com/yusing/go-proxy/internal/task"
U "github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils"
F "github.com/yusing/go-proxy/internal/utils/functional"
"github.com/yusing/go-proxy/internal/watcher"
"github.com/yusing/go-proxy/internal/watcher/events"
"gopkg.in/yaml.v3"
)
type Config struct {
@@ -68,7 +67,8 @@ func Load() (*Config, E.Error) {
}
func Validate(data []byte) E.Error {
return U.ValidateYaml(U.GetSchema(common.ConfigSchemaPath), data)
var model *types.Config
return utils.DeserializeYAML(data, model)
}
func MatchDomains() []string {
@@ -160,14 +160,8 @@ func (cfg *Config) load() E.Error {
E.LogFatal(errMsg, err, &logger)
}
if !common.NoSchemaValidation {
if err := Validate(data); err != nil {
E.LogFatal(errMsg, err, &logger)
}
}
model := types.DefaultConfig()
if err := E.From(yaml.Unmarshal(data, model)); err != nil {
if err := utils.DeserializeYAML(data, model); err != nil {
E.LogFatal(errMsg, err, &logger)
}
@@ -176,7 +170,7 @@ func (cfg *Config) load() E.Error {
errs.Add(entrypoint.SetMiddlewares(model.Entrypoint.Middlewares))
errs.Add(entrypoint.SetAccessLogger(cfg.task, model.Entrypoint.AccessLog))
errs.Add(cfg.initNotification(model.Providers.Notification))
errs.Add(cfg.initAutoCert(&model.AutoCert))
errs.Add(cfg.initAutoCert(model.AutoCert))
errs.Add(cfg.loadRouteProviders(&model.Providers))
cfg.value = model

View File

@@ -2,13 +2,13 @@ package types
type (
AutoCertConfig struct {
Email string `json:"email,omitempty" yaml:"email"`
Domains []string `json:"domains,omitempty" yaml:",flow"`
CertPath string `json:"cert_path,omitempty" yaml:"cert_path"`
KeyPath string `json:"key_path,omitempty" yaml:"key_path"`
ACMEKeyPath string `json:"acme_key_path,omitempty" yaml:"acme_key_path"`
Provider string `json:"provider,omitempty" yaml:"provider"`
Options AutocertProviderOpt `json:"options,omitempty" yaml:",flow"`
Email string `json:"email,omitempty" validate:"email"`
Domains []string `json:"domains,omitempty"`
CertPath string `json:"cert_path,omitempty" validate:"omitempty,filepath"`
KeyPath string `json:"key_path,omitempty" validate:"omitempty,filepath"`
ACMEKeyPath string `json:"acme_key_path,omitempty" validate:"omitempty,filepath"`
Provider string `json:"provider,omitempty"`
Options AutocertProviderOpt `json:"options,omitempty"`
}
AutocertProviderOpt map[string]any
)

View File

@@ -4,21 +4,21 @@ import "github.com/yusing/go-proxy/internal/net/http/accesslog"
type (
Config struct {
AutoCert AutoCertConfig `json:"autocert" yaml:",flow"`
Entrypoint Entrypoint `json:"entrypoint" yaml:",flow"`
Providers Providers `json:"providers" yaml:",flow"`
MatchDomains []string `json:"match_domains" yaml:"match_domains"`
Homepage HomepageConfig `json:"homepage" yaml:"homepage"`
TimeoutShutdown int `json:"timeout_shutdown" yaml:"timeout_shutdown"`
AutoCert *AutoCertConfig `json:"autocert"`
Entrypoint Entrypoint `json:"entrypoint"`
Providers Providers `json:"providers"`
MatchDomains []string `json:"match_domains" validate:"dive,fqdn"`
Homepage HomepageConfig `json:"homepage"`
TimeoutShutdown int `json:"timeout_shutdown" validate:"gte=0"`
}
Providers struct {
Files []string `json:"include" yaml:"include"`
Docker map[string]string `json:"docker" yaml:"docker"`
Notification []NotificationConfig `json:"notification" yaml:"notification"`
Files []string `json:"include" validate:"dive,filepath"`
Docker map[string]string `json:"docker" validate:"dive,unix_addr|url"`
Notification []NotificationConfig `json:"notification"`
}
Entrypoint struct {
Middlewares []map[string]any `json:"middlewares" yaml:"middlewares"`
AccessLog *accesslog.Config `json:"access_log" yaml:"access_log"`
Middlewares []map[string]any `json:"middlewares"`
AccessLog *accesslog.Config `json:"access_log"`
}
NotificationConfig map[string]any
)

View File

@@ -1,5 +1,5 @@
package types
type HomepageConfig struct {
UseDefaultCategories bool `json:"use_default_categories" yaml:"use_default_categories"`
UseDefaultCategories bool `json:"use_default_categories"`
}