| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package subconfigs
-
- import "fmt"
-
- // ServiceConfig 微服务配置
- type ServiceConfig struct {
- BaseConfig
- ServiceName string `yaml:"service_name"`
- ServiceVersion string `yaml:"service_version"`
- ServiceTag string `yaml:"service_tags"`
- Port int `yaml:"port"`
- ReadTimeout int `yaml:"read_timeout"`
- WriteTimeout int `yaml:"write_timeout"`
- IdleTimeout int `yaml:"idle_timeout"`
- TrustedProxies string `yaml:"trusted_proxies"`
- InstanceName string `yaml:"instance-name"`
- Env string `yaml:"env"`
- }
-
- func NewServiceConfig() *ServiceConfig {
- return &ServiceConfig{}
- }
-
- func (c *ServiceConfig) SetDefaults() {
- c.ServiceName = "myService"
- c.Port = 8080
- c.ReadTimeout = 30
- c.WriteTimeout = 30
- c.IdleTimeout = 60
- }
-
- func (c *ServiceConfig) Load(data map[string]interface{}) error {
- return c.LoadFromYAML(data, c)
- }
-
- func (c *ServiceConfig) Validate() error {
- if c.ServiceName == "" {
- return fmt.Errorf("service name is required")
- }
- if c.Port <= 0 || c.Port > 65535 {
- return fmt.Errorf("invalid port: %d", c.Port)
- }
- return nil
- }
-
- // 自动注册
- func init() {
- Register("service", &ServiceConfig{})
- }
|