conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. "go-common/library/log/infoc"
  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. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. // Conf is
  20. var (
  21. confPath string
  22. client *conf.Client
  23. Conf = &Config{}
  24. )
  25. //Config struct
  26. type Config struct {
  27. // Env
  28. Env string
  29. AutoLimit int64
  30. DMRegion []int16
  31. // interface XLog
  32. XLog *log.Config
  33. Bnj2019 *Bnj2019
  34. // infoc
  35. InfocCoin *infoc.Config
  36. InfocView *infoc.Config
  37. InfocRelate *infoc.Config
  38. UseractPub *databus.Config
  39. DislikePub *databus.Config
  40. // tick time
  41. Tick xtime.Duration
  42. // vip tick
  43. VipTick xtime.Duration
  44. // tracer
  45. Tracer *trace.Config
  46. // httpClinet
  47. HTTPClient *bm.ClientConfig
  48. // httpWrite
  49. HTTPWrite *bm.ClientConfig
  50. // httpbangumi
  51. HTTPBangumi *bm.ClientConfig
  52. // httpaudio
  53. HTTPAudio *bm.ClientConfig
  54. // http
  55. BM *HTTPServers
  56. // httpAd
  57. HTTPAD *bm.ClientConfig
  58. // httpGame
  59. HTTPGame *bm.ClientConfig
  60. // HTTPAsync
  61. HTTPAsync *bm.ClientConfig
  62. // HTTPGameAsync
  63. HTTPGameAsync *bm.ClientConfig
  64. // httpClinet
  65. HTTPSearch *bm.ClientConfig
  66. // host
  67. Host *Host
  68. // rpc client
  69. AccountRPC *rpc.ClientConfig
  70. ArchiveRPC *rpc.ClientConfig
  71. TagRPC *rpc.ClientConfig
  72. AssistRPC *rpc.ClientConfig
  73. HisRPC *rpc.ClientConfig
  74. ResourceRPC *rpc.ClientConfig
  75. RelationRPC *rpc.ClientConfig
  76. FavoriteRPC *rpc.ClientConfig
  77. CoinRPC *rpc.ClientConfig
  78. DMRPC *rpc.ClientConfig
  79. ActivityRPC *rpc.ClientConfig
  80. LocationRPC *rpc.ClientConfig
  81. // db
  82. MySQL *MySQL
  83. // ecode
  84. Ecode *ecode.Config
  85. // mc
  86. Memcache *Memcache
  87. // PlayURL
  88. PlayURL *PlayURL
  89. // 相关推荐秒开个数
  90. RelateCnt int
  91. RelateGray int64
  92. // buildLimit
  93. BuildLimit *BuildLimit
  94. // Warden Client
  95. ThumbupClient *warden.ClientConfig
  96. }
  97. // Bnj2019 is
  98. type Bnj2019 struct {
  99. Tick xtime.Duration
  100. MainAid int64
  101. AdAv int64
  102. AidList []int64
  103. ElecBigText string
  104. ElecSmallText string
  105. WhiteMids []int64
  106. FakeElec int
  107. }
  108. // HTTPServers Http Servers
  109. type HTTPServers struct {
  110. Outer *bm.ServerConfig
  111. }
  112. // Host struct
  113. type Host struct {
  114. Bangumi string
  115. APICo string
  116. Activity string
  117. Elec string
  118. AD string
  119. Data string
  120. Archive string
  121. APILiveCo string
  122. Game string
  123. VIP string
  124. AI string
  125. Search string
  126. Bvcvod string
  127. }
  128. // MySQL struct
  129. type MySQL struct {
  130. Show *sql.Config
  131. Manager *sql.Config
  132. }
  133. // Memcache struct
  134. type Memcache struct {
  135. Archive *struct {
  136. *memcache.Config
  137. ArchiveExpire xtime.Duration
  138. RelateExpire xtime.Duration
  139. AddonExpire xtime.Duration
  140. RecommedExpire xtime.Duration
  141. }
  142. }
  143. // PlayURL playurl token's secret.
  144. type PlayURL struct {
  145. Secret string
  146. }
  147. // BuildLimt for build limit
  148. type BuildLimit struct {
  149. CooperationIOS int
  150. CooperationAndroid int
  151. }
  152. func init() {
  153. flag.StringVar(&confPath, "conf", "", "config path")
  154. }
  155. // Init init conf
  156. func Init() error {
  157. if confPath != "" {
  158. return local()
  159. }
  160. return remote()
  161. }
  162. func local() (err error) {
  163. _, err = toml.DecodeFile(confPath, &Conf)
  164. return
  165. }
  166. func remote() (err error) {
  167. if client, err = conf.New(); err != nil {
  168. return
  169. }
  170. if err = load(); err != nil {
  171. return
  172. }
  173. client.Watch("app-view.toml")
  174. go func() {
  175. for range client.Event() {
  176. log.Info("config reload")
  177. if load() != nil {
  178. log.Error("config reload error (%v)", err)
  179. }
  180. }
  181. }()
  182. return
  183. }
  184. func load() (err error) {
  185. var (
  186. s string
  187. ok bool
  188. tmpConf *Config
  189. )
  190. if s, ok = client.Toml2(); !ok {
  191. return errors.New("load config center error")
  192. }
  193. if _, err = toml.Decode(s, &tmpConf); err != nil {
  194. return errors.New("could not decode config")
  195. }
  196. *Conf = *tmpConf
  197. return
  198. }