conf.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/sql"
  8. xlog "go-common/library/log"
  9. "go-common/library/log/infoc"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. // Config .
  18. type Config struct {
  19. // Env
  20. Env string
  21. // App
  22. App *bm.App
  23. // Xlog is go-common log.
  24. Xlog *xlog.Config
  25. // Tracer .
  26. Tracer *trace.Config
  27. // ArchiveSub databus
  28. ArticleSub *databus.Config
  29. // ArticleStatSub databus
  30. ArticleStatSub *databus.Config
  31. // LikeStatSub databus
  32. LikeStatSub *databus.Config
  33. ReplyStatSub *databus.Config
  34. FavoriteStatSub *databus.Config
  35. CoinStatSub *databus.Config
  36. // DynamicDbus pub databus
  37. DynamicDbus *databus.Config
  38. // BM
  39. BM *bm.ServerConfig
  40. // HTTPClient .
  41. HTTPClient *bm.ClientConfig
  42. GameHTTPClient *bm.ClientConfig
  43. // RPC .
  44. ArticleRPC *rpc.ClientConfig
  45. TagRPC *rpc.ClientConfig
  46. // DB
  47. DB *sql.Config
  48. // Redis
  49. Redis *redis.Config
  50. // SMS text message.
  51. SMS *sms
  52. // CheatInfoc
  53. CheatInfoc *infoc.Config
  54. // ReadInfoc
  55. ReadInfoc *infoc.Config
  56. // article interface redis
  57. ArtRedis *redis.Config
  58. // Job params
  59. Job *job
  60. // Sitemap
  61. Sitemap Sitemap
  62. }
  63. // Sitemap .
  64. type Sitemap struct {
  65. Interval int64
  66. Size int
  67. }
  68. type job struct {
  69. ViewCacheTTL xtime.Duration
  70. DupViewCacheTTL xtime.Duration
  71. UpdateDbInterval xtime.Duration
  72. UpdateSortInterval xtime.Duration
  73. GameCacheExpire xtime.Duration
  74. ListReadCountInterval xtime.Duration
  75. HotspotInterval xtime.Duration
  76. HotspotForceInterval xtime.Duration
  77. ExpireSortArts xtime.Duration
  78. TTLSortArts xtime.Duration
  79. SortLimitTime xtime.Duration
  80. RecommendExpire xtime.Duration
  81. Words int64
  82. StatDays int64
  83. ActLikeURL string
  84. FlowURL string
  85. MaxNewArtsNum int64
  86. MaxSortArtsNum int64
  87. }
  88. type sms struct {
  89. Phone string
  90. Token string
  91. }
  92. var (
  93. confPath string
  94. client *conf.Client
  95. // Conf config
  96. Conf = &Config{}
  97. )
  98. func init() {
  99. flag.StringVar(&confPath, "conf", "", "config path")
  100. }
  101. // Init .
  102. func Init() (err error) {
  103. if confPath != "" {
  104. return local()
  105. }
  106. return remote()
  107. }
  108. func local() (err error) {
  109. _, err = toml.DecodeFile(confPath, &Conf)
  110. return
  111. }
  112. func remote() (err error) {
  113. if client, err = conf.New(); err != nil {
  114. return
  115. }
  116. if err = load(); err != nil {
  117. return
  118. }
  119. go func() {
  120. for range client.Event() {
  121. xlog.Info("config reload")
  122. if load() != nil {
  123. xlog.Error("config reload err")
  124. }
  125. }
  126. }()
  127. return
  128. }
  129. func load() (err error) {
  130. var (
  131. s string
  132. ok bool
  133. tmpConf *Config
  134. )
  135. if s, ok = client.Toml2(); !ok {
  136. return errors.New("load config center error")
  137. }
  138. if _, err = toml.Decode(s, &tmpConf); err != nil {
  139. return errors.New("could not decode config")
  140. }
  141. *Conf = *tmpConf
  142. return
  143. }