conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/netutil"
  11. "go-common/library/net/rpc"
  12. "go-common/library/queue/databus"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // global var
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config config set
  23. type Config struct {
  24. // base
  25. // elk
  26. Log *log.Config
  27. // http
  28. BM *bm.ServerConfig
  29. // memcache
  30. Memcache *memcache.Config
  31. // MySQL
  32. MySQL *sql.Config
  33. // Databus
  34. DataBus *DataSource
  35. // ProPerties
  36. Properties *Properties
  37. // http client
  38. HTTPClient *bm.ClientConfig
  39. // Backoff retries config
  40. Backoff *netutil.BackoffConfig
  41. PointRPC *rpc.ClientConfig
  42. }
  43. // DataSource databus source
  44. type DataSource struct {
  45. OldVipBinlog *databus.Config
  46. PointBinlog *databus.Config
  47. PointUpdate *databus.Config
  48. }
  49. // Properties def.
  50. type Properties struct {
  51. MaxRetries int
  52. PointConsumeNotify map[string]string
  53. NotifyCacheDelURL []string
  54. }
  55. // HTTPServers Http Servers
  56. type HTTPServers struct {
  57. Outer *bm.ServerConfig
  58. Inner *bm.ServerConfig
  59. Local *bm.ServerConfig
  60. }
  61. func init() {
  62. flag.StringVar(&confPath, "conf", "", "default config path")
  63. }
  64. // Init init conf
  65. func Init() error {
  66. if confPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(confPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Toml2(); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }