refactor, fix reload error when using agents, and other small improvements

This commit is contained in:
yusing
2025-02-11 12:15:51 +08:00
parent b1f72620dc
commit 429a77de8e
10 changed files with 139 additions and 105 deletions

View File

@@ -3,6 +3,7 @@ package types
import (
"context"
"regexp"
"sync"
"github.com/go-playground/validator/v10"
"github.com/yusing/go-proxy/agent/pkg/agent"
@@ -25,8 +26,8 @@ type (
}
Providers struct {
Files []string `json:"include" yaml:"include,omitempty" validate:"dive,filepath"`
Docker map[string]string `json:"docker" yaml:"docker,omitempty" validate:"dive,unix_addr|url"`
Agents []agent.AgentConfig `json:"agents" yaml:"agents,omitempty"`
Docker map[string]string `json:"docker" yaml:"docker,omitempty" validate:"non_empty_docker_keys,dive,unix_addr|url"`
Agents []*agent.AgentConfig `json:"agents" yaml:"agents,omitempty"`
Notification []notif.NotificationConfig `json:"notification" yaml:"notification,omitempty"`
}
Entrypoint struct {
@@ -40,9 +41,15 @@ type (
Statistics() map[string]any
RouteProviderList() []string
Context() context.Context
GetAgent(agentDockerHost string) (*agent.AgentConfig, bool)
}
)
var (
instance ConfigInstance
instanceMu sync.RWMutex
)
func DefaultConfig() *Config {
return &Config{
TimeoutShutdown: 3,
@@ -52,6 +59,24 @@ func DefaultConfig() *Config {
}
}
func GetInstance() ConfigInstance {
instanceMu.RLock()
defer instanceMu.RUnlock()
return instance
}
func SetInstance(cfg ConfigInstance) {
instanceMu.Lock()
defer instanceMu.Unlock()
instance = cfg
}
func HasInstance() bool {
instanceMu.RLock()
defer instanceMu.RUnlock()
return instance != nil
}
func Validate(data []byte) E.Error {
var model Config
return utils.DeserializeYAML(data, &model)
@@ -70,4 +95,13 @@ func init() {
}
return true
})
utils.MustRegisterValidation("non_empty_docker_keys", func(fl validator.FieldLevel) bool {
m := fl.Field().Interface().(map[string]string)
for k := range m {
if k == "" {
return false
}
}
return true
})
}