github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/registry/config.go (about) 1 package registry 2 3 import ( 4 "context" 5 "crypto/tls" 6 "strings" 7 "time" 8 9 "github.com/volts-dev/volts/config" 10 "github.com/volts-dev/volts/logger" 11 ) 12 13 type servicesKey struct{} 14 15 type ( 16 Option func(*Config) 17 WatchOptions func(*WatchConfig) error 18 19 RegisterConfig struct { 20 TTL time.Duration 21 // Other options for implementations of the interface 22 // can be stored in a context 23 Context context.Context 24 } 25 26 DeregisterConfig struct { 27 Context context.Context 28 } 29 30 GetConfig struct { 31 Context context.Context 32 } 33 34 ListConfig struct { 35 Context context.Context 36 } 37 38 Config struct { 39 config.Config `field:"-"` 40 Name string `field:"-"` // 41 PrefixName string `field:"-"` // config prefix name/path in config file 42 Logger logger.ILogger `field:"-"` // 实例 43 Context context.Context `field:"-"` 44 LocalServices []*Service `field:"-"` // current service information 45 Addrs []string `field:"-"` 46 TlsConfig *tls.Config `field:"-"` 47 48 Timeout time.Duration 49 Secure bool 50 TTL time.Duration `field:"ttl"` 51 } 52 53 WatchConfig struct { 54 // Specify a service to watch 55 // If blank, the watch is for all services 56 Service string 57 // Other options for implementations of the interface 58 // can be stored in a context 59 Context context.Context 60 } 61 ) 62 63 // new and init a config 64 func NewConfig(opts ...Option) *Config { 65 cfg := &Config{ 66 Logger: log, 67 Context: context.Background(), 68 Timeout: time.Millisecond * 100, 69 } 70 cfg.Init(opts...) 71 config.Register(cfg) 72 return cfg 73 } 74 75 func (self *Config) String() string { 76 if len(self.PrefixName) > 0 { 77 return strings.Join([]string{self.PrefixName, "registry"}, ".") 78 } 79 80 if len(self.Name) > 0 { 81 return strings.Join([]string{"registry", self.Name}, ".") 82 } 83 84 return "" 85 } 86 87 func (self *Config) Init(opts ...Option) { 88 for _, opt := range opts { 89 if opt != nil { 90 opt(self) 91 } 92 } 93 } 94 95 func (self *Config) Load() error { 96 return self.LoadToModel(self) 97 } 98 99 func (self *Config) Save(immed ...bool) error { 100 return self.SaveFromModel(self, immed...) 101 } 102 103 func Logger() logger.ILogger { 104 return log 105 } 106 107 func Debug() Option { 108 return func(cfg *Config) { 109 cfg.Debug = true 110 } 111 } 112 113 // Addrs is the registry addresses to use 114 func Addrs(addrs ...string) Option { 115 return func(cfg *Config) { 116 cfg.Addrs = addrs 117 } 118 } 119 120 func Timeout(t time.Duration) Option { 121 return func(cfg *Config) { 122 cfg.Timeout = t 123 } 124 } 125 126 // Secure communication with the registry 127 func Secure(b bool) Option { 128 return func(cfg *Config) { 129 cfg.Secure = b 130 } 131 } 132 133 // Specify TLS Config 134 func TLSConfig(t *tls.Config) Option { 135 return func(cfg *Config) { 136 cfg.TlsConfig = t 137 } 138 } 139 140 func RegisterTTL(t time.Duration) Option { 141 return func(cfg *Config) { 142 cfg.TTL = t 143 } 144 } 145 146 // change registry name 147 func WithName(name string) Option { 148 return func(cfg *Config) { 149 //cfg.Unregister(cfg) 150 cfg.Name = name 151 //cfg.Register(cfg) 152 } 153 } 154 155 // 修改Config.json的路径 156 func WithConfigPrefixName(prefixName string) Option { 157 return func(cfg *Config) { 158 cfg.Unregister(cfg) 159 cfg.PrefixName = prefixName 160 cfg.Register(cfg) 161 } 162 }