conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/hbase.v2"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // global var
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config config set
  24. type Config struct {
  25. // base
  26. // env
  27. Env string
  28. // elk
  29. Log *log.Config
  30. // HTTPClient .
  31. HTTPClient *bm.ClientConfig
  32. // http
  33. BM *HTTPServers
  34. // tracer
  35. Tracer *trace.Config
  36. // MySQL
  37. MySQL *sql.Config
  38. // hbase
  39. HBase *HBaseConfig
  40. BlackListHBase *HBaseConfig
  41. // databuse
  42. LiveRoomSub *databus.Config // 开播提醒
  43. LiveCommonSub *databus.Config // 直播通用
  44. // push
  45. Push *push
  46. // redis
  47. Redis *Redis
  48. }
  49. // HBaseConfig extra hbase config for compatible
  50. type HBaseConfig struct {
  51. *hbase.Config
  52. WriteTimeout xtime.Duration
  53. ReadTimeout xtime.Duration
  54. }
  55. // HTTPServers Http Servers
  56. type HTTPServers struct {
  57. Inner *bm.ServerConfig
  58. Local *bm.ServerConfig
  59. }
  60. type push struct {
  61. MultiAPI string
  62. AppID int
  63. BusinessID int
  64. BusinessToken string
  65. LinkType int
  66. PushRetryTimes int
  67. PushOnceLimit int
  68. DefaultCopyWriting string
  69. SpecialCopyWriting string
  70. ConsumerProcNum int
  71. IntervalLimit int
  72. PushFilterIgnores struct {
  73. Smooth, Limit []int
  74. }
  75. }
  76. // Redis Redis.PushInterval config
  77. type Redis struct {
  78. PushInterval *struct {
  79. *redis.Config
  80. Expire xtime.Duration
  81. }
  82. }
  83. func init() {
  84. flag.StringVar(&confPath, "conf", "", "default config path")
  85. }
  86. // Init init conf
  87. func Init() error {
  88. if confPath != "" {
  89. return local()
  90. }
  91. return remote()
  92. }
  93. func local() (err error) {
  94. _, err = toml.DecodeFile(confPath, &Conf)
  95. return
  96. }
  97. func remote() (err error) {
  98. if client, err = conf.New(); err != nil {
  99. return
  100. }
  101. if err = load(); err != nil {
  102. return
  103. }
  104. go func() {
  105. for range client.Event() {
  106. log.Info("config reload")
  107. if load() != nil {
  108. log.Error("config reload error (%v)", err)
  109. }
  110. }
  111. }()
  112. return
  113. }
  114. func load() (err error) {
  115. var (
  116. s string
  117. ok bool
  118. tmpConf *Config
  119. )
  120. if s, ok = client.Toml2(); !ok {
  121. return errors.New("load config center error")
  122. }
  123. if _, err = toml.Decode(s, &tmpConf); err != nil {
  124. return errors.New("could not decode config")
  125. }
  126. *Conf = *tmpConf
  127. return
  128. }