config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package conf
  2. import (
  3. "errors"
  4. "github.com/BurntSushi/toml"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. )
  9. type BroadcastProxyConfig struct {
  10. Perf string `toml:"perf"`
  11. Log *log.Config
  12. Http *HttpConfig
  13. Backend *BackendConfig
  14. ZooKeeper *ZooKeeperConfig
  15. Ipip *IpipConfig
  16. Dispatch *DispatchConfig
  17. Sven *SvenConfig
  18. }
  19. type HttpConfig struct {
  20. Address string
  21. }
  22. type BackendConfig struct {
  23. MaxIdleConnsPerHost int
  24. ProbePath string
  25. BackendServer []string
  26. ProbeSample int
  27. }
  28. type ZooKeeperConfig struct {
  29. Address []string
  30. Timeout xtime.Duration
  31. ConfigPath string
  32. }
  33. type SinaIPConfig struct {
  34. Data string
  35. }
  36. type IpipConfig struct {
  37. V4 string
  38. V6 string
  39. }
  40. type DispatchConfig struct {
  41. MaxLimit int
  42. DefaultDomain string
  43. WildcardDomainSuffix string
  44. FileName string
  45. }
  46. type SvenConfig struct {
  47. TreeID string
  48. Zone string
  49. Env string
  50. Build string
  51. Token string
  52. }
  53. func NewBroadcastProxyConfig(file string) (*BroadcastProxyConfig, error) {
  54. config := new(BroadcastProxyConfig)
  55. if file != "" {
  56. if err := config.local(file); err != nil {
  57. return nil, err
  58. }
  59. } else {
  60. if err := config.remote(); err != nil {
  61. return nil, err
  62. }
  63. }
  64. return config, nil
  65. }
  66. func (config *BroadcastProxyConfig) local(filename string) (err error) {
  67. _, err = toml.DecodeFile(filename, config)
  68. return
  69. }
  70. func (config *BroadcastProxyConfig) remote() error {
  71. client, err := conf.New()
  72. if err != nil {
  73. return err
  74. }
  75. if err = config.load(client); err != nil {
  76. return err
  77. }
  78. go func() {
  79. for range client.Event() {
  80. log.Info("config event")
  81. }
  82. }()
  83. return nil
  84. }
  85. func (config *BroadcastProxyConfig) load(c *conf.Client) error {
  86. s, ok := c.Value("live-broadcast-proxy.toml")
  87. if !ok {
  88. return errors.New("load config center error")
  89. }
  90. if _, err := toml.Decode(s, config); err != nil {
  91. return errors.New("could not decode config")
  92. }
  93. return nil
  94. }