conf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Tracer *trace.Config
  30. Redis *redis.Config
  31. Memcache *memcache.Config
  32. DB *DB
  33. Ecode *ecode.Config
  34. // rpc
  35. DynamicRPC *rpc.ClientConfig
  36. FavoriteRPC *rpc.ClientConfig
  37. ArchiveRPC *rpc.ClientConfig
  38. // Host
  39. Host Host
  40. // HTTP client
  41. HTTPClient *bm.ClientConfig
  42. MessageHTTPClient *bm.ClientConfig
  43. // Rule
  44. Rule *Rule
  45. //Push push urls
  46. Push *Push
  47. //Message
  48. Message Message
  49. // App
  50. App *bm.App
  51. // databus
  52. ArchiveNotifySub *databus.Config
  53. // Warden Client
  54. ArcClient *warden.ClientConfig
  55. }
  56. // Push push.
  57. type Push struct {
  58. BusinessID int
  59. BusinessToken string
  60. PartSize int
  61. RetryTimes int
  62. Title string
  63. BodyDefault string
  64. BodySpecial string
  65. OnlyMids string
  66. }
  67. // Message .
  68. type Message struct {
  69. URL string
  70. MC string
  71. }
  72. // Rule .
  73. type Rule struct {
  74. BroadFeed int
  75. SleepInterval time.Duration
  76. Before time.Duration
  77. ScoreSleep time.Duration
  78. AlertTitle string
  79. AlertBodyDefault string
  80. AlertBodySpecial string
  81. CoinPercent float64
  82. FavPercent float64
  83. DmPercent float64
  84. ReplyPercent float64
  85. ViewPercent float64
  86. LikePercent float64
  87. SharePercent float64
  88. NewDay float64
  89. NewPercent float64
  90. }
  91. // Host remote host
  92. type Host struct {
  93. API string
  94. }
  95. // DB define MySQL config
  96. type DB struct {
  97. Esports *sql.Config
  98. }
  99. func init() {
  100. flag.StringVar(&confPath, "conf", "", "default config path")
  101. }
  102. // Init init conf
  103. func Init() error {
  104. if confPath != "" {
  105. return local()
  106. }
  107. return remote()
  108. }
  109. func local() (err error) {
  110. _, err = toml.DecodeFile(confPath, &Conf)
  111. return
  112. }
  113. func remote() (err error) {
  114. if client, err = conf.New(); err != nil {
  115. return
  116. }
  117. if err = load(); err != nil {
  118. return
  119. }
  120. go func() {
  121. for range client.Event() {
  122. log.Info("config reload")
  123. if load() != nil {
  124. log.Error("config reload error (%v)", err)
  125. }
  126. }
  127. }()
  128. return
  129. }
  130. func load() (err error) {
  131. var (
  132. s string
  133. ok bool
  134. tmpConf *Config
  135. )
  136. if s, ok = client.Toml2(); !ok {
  137. return errors.New("load config center error")
  138. }
  139. if _, err = toml.Decode(s, &tmpConf); err != nil {
  140. return errors.New("could not decode config")
  141. }
  142. *Conf = *tmpConf
  143. return
  144. }