conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. xtime "time"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/elastic"
  10. "go-common/library/database/sql"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. // Conf conf
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config so config
  26. type Config struct {
  27. // interface Log
  28. Log *log.Config
  29. //HTTPClient
  30. HTTPClient *bm.ClientConfig
  31. // BM
  32. BM *bm.ServerConfig
  33. // rpc
  34. ArchiveRPC *rpc.ClientConfig
  35. ArticleRPC *rpc.ClientConfig
  36. CoinRPC *rpc.ClientConfig
  37. ActRPC *rpc.ClientConfig
  38. // grpc
  39. AccClient *warden.ClientConfig
  40. // DB
  41. MySQL *MySQL
  42. // mc
  43. Memcache *Memcache
  44. // redis
  45. Redis *Redis
  46. // databus
  47. ActSub *databus.Config
  48. BnjSub *databus.Config
  49. // vip binlog databus
  50. //VipSub *databus.Config
  51. KfcSub *databus.Config
  52. // Interval
  53. Interval *interval
  54. // Rule
  55. Rule *rule
  56. // Host
  57. Host *host
  58. // Elastic
  59. Elastic *elastic.Config
  60. // bnj
  61. Bnj2019 *bnj2019
  62. }
  63. type bnj2019 struct {
  64. GameCancel int
  65. LID int64
  66. StartTime xtime.Time
  67. TimelinePic string
  68. H5TimelinePic string
  69. MsgSpec string
  70. MidLimit int64
  71. WxKey string
  72. WxTitle string
  73. WxUser string
  74. Time []*struct {
  75. Score int64
  76. Second int64
  77. Step int
  78. WxMsg string
  79. MsgTitle string
  80. MsgMc string
  81. Msg string
  82. }
  83. Message []*struct {
  84. Start xtime.Time
  85. Title string
  86. Content string
  87. Mc string
  88. }
  89. }
  90. type interval struct {
  91. CoinInterval time.Duration
  92. QueryInterval time.Duration
  93. ObjStatInterval time.Duration
  94. ViewRankInterval time.Duration
  95. KingStoryInterval time.Duration
  96. }
  97. // MySQL is db config.
  98. type MySQL struct {
  99. Like *sql.Config
  100. }
  101. // Redis config
  102. type Redis struct {
  103. *redis.Config
  104. Expire time.Duration
  105. }
  106. // Memcache config
  107. type Memcache struct {
  108. Like *memcache.Config
  109. LikeExpire time.Duration
  110. TimeFinishExpire time.Duration
  111. LessTimeExpire time.Duration
  112. }
  113. type rule struct {
  114. BroadcastCid int64
  115. BroadcastSid int64
  116. ArcObjStatSid int64
  117. ArtObjStatSid int64
  118. KingStorySid int64
  119. EleLotteryID int64
  120. }
  121. type host struct {
  122. APICo string
  123. Activity string
  124. MsgCo string
  125. }
  126. func init() {
  127. flag.StringVar(&confPath, "conf", "", "default config path")
  128. }
  129. // Init init config.
  130. func Init() (err error) {
  131. if confPath != "" {
  132. _, err = toml.DecodeFile(confPath, &Conf)
  133. return
  134. }
  135. err = remote()
  136. return
  137. }
  138. func remote() (err error) {
  139. if client, err = conf.New(); err != nil {
  140. return
  141. }
  142. if err = load(); err != nil {
  143. return
  144. }
  145. client.Watch("activity-job.toml")
  146. go func() {
  147. for range client.Event() {
  148. log.Info("config reload")
  149. if load() != nil {
  150. log.Error("config reload error (%v)", err)
  151. }
  152. }
  153. }()
  154. return
  155. }
  156. func load() (err error) {
  157. var (
  158. s string
  159. ok bool
  160. tmpConf = &Config{}
  161. )
  162. if s, ok = client.Toml2(); !ok {
  163. return errors.New("load config center error")
  164. }
  165. if _, err = toml.Decode(s, tmpConf); err != nil {
  166. return errors.New("could not decode config")
  167. }
  168. *Conf = *tmpConf
  169. return
  170. }