Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service_config.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package subconfigs
  2. import "fmt"
  3. // ServiceConfig 微服务配置
  4. type ServiceConfig struct {
  5. BaseConfig
  6. ServiceName string `yaml:"service_name"`
  7. ServiceVersion string `yaml:"service_version"`
  8. ServiceTag string `yaml:"service_tags"`
  9. Port int `yaml:"port"`
  10. ReadTimeout int `yaml:"read_timeout"`
  11. WriteTimeout int `yaml:"write_timeout"`
  12. IdleTimeout int `yaml:"idle_timeout"`
  13. TrustedProxies string `yaml:"trusted_proxies"`
  14. InstanceName string `yaml:"instance-name"`
  15. Env string `yaml:"env"`
  16. }
  17. func NewServiceConfig() *ServiceConfig {
  18. return &ServiceConfig{}
  19. }
  20. func (c *ServiceConfig) SetDefaults() {
  21. c.ServiceName = "myService"
  22. c.Port = 8080
  23. c.ReadTimeout = 30
  24. c.WriteTimeout = 30
  25. c.IdleTimeout = 60
  26. }
  27. func (c *ServiceConfig) Load(data map[string]interface{}) error {
  28. return c.LoadFromYAML(data, c)
  29. }
  30. func (c *ServiceConfig) Validate() error {
  31. if c.ServiceName == "" {
  32. return fmt.Errorf("service name is required")
  33. }
  34. if c.Port <= 0 || c.Port > 65535 {
  35. return fmt.Errorf("invalid port: %d", c.Port)
  36. }
  37. return nil
  38. }
  39. // 自动注册
  40. func init() {
  41. Register("service", &ServiceConfig{})
  42. }