conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/interface/main/app-feed/model/feed"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. "go-common/library/log/infoc"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus"
  18. xtime "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. )
  21. var (
  22. confPath string
  23. Conf = &Config{}
  24. client *conf.Client
  25. )
  26. type Config struct {
  27. // env
  28. Env string
  29. // infoc log2
  30. ShowInfoc2 *infoc.Config
  31. TagInfoc2 *infoc.Config
  32. RedirectInfoc2 *infoc.Config
  33. // show XLog
  34. XLog *log.Config
  35. // tick time
  36. Tick xtime.Duration
  37. // tracer
  38. Tracer *trace.Config
  39. // httpClinet
  40. HTTPClient *bm.ClientConfig
  41. // httpAsyn
  42. HTTPClientAsyn *bm.ClientConfig
  43. // httpData
  44. HTTPData *bm.ClientConfig
  45. // httpTag
  46. HTTPTag *bm.ClientConfig
  47. // httpAd
  48. HTTPAd *bm.ClientConfig
  49. // httpActivity
  50. HTTPActivity *bm.ClientConfig
  51. // httpBangumi
  52. HTTPBangumi *bm.ClientConfig
  53. // httpShow
  54. HTTPShow *bm.ClientConfig
  55. // httpDynamic
  56. HTTPDynamic *bm.ClientConfig
  57. // httpClinet
  58. HTTPSearch *bm.ClientConfig
  59. // rpc Location
  60. LocationRPC *rpc.ClientConfig
  61. // http
  62. BM *HTTPServers
  63. // host
  64. Host *Host
  65. // db
  66. MySQL *MySQL
  67. // redis
  68. Redis *Redis
  69. // mc
  70. Memcache *Memcache
  71. // rpc client
  72. AccountRPC *rpc.ClientConfig
  73. RelationRPC *rpc.ClientConfig
  74. ArchiveRPC *rpc.ClientConfig
  75. FeedRPC *rpc.ClientConfig
  76. TagRPC *rpc.ClientConfig
  77. ArticleRPC *rpc.ClientConfig
  78. ResourceRPC *rpc.ClientConfig
  79. // databus
  80. DislikeDatabus *databus.Config
  81. // ecode
  82. Ecode *ecode.Config
  83. // feed
  84. Feed *Feed
  85. // bnj2018
  86. Bnj *BnjConfig
  87. // BroadcastRPC grpc
  88. PGCRPC *warden.ClientConfig
  89. // autoplay mids
  90. AutoPlayMids []int64
  91. }
  92. // HTTPServers Http Servers
  93. type HTTPServers struct {
  94. Outer *bm.ServerConfig
  95. }
  96. // BnjConfig 2018拜年祭配置
  97. type BnjConfig struct {
  98. TabImg string
  99. TabID int64
  100. BeginTime string
  101. }
  102. type Host struct {
  103. LiveAPI string
  104. Bangumi string
  105. Data string
  106. Hetongzi string
  107. APICo string
  108. Ad string
  109. Activity string
  110. Rank string
  111. Show string
  112. Dynamic string
  113. DynamicCo string
  114. BigData string
  115. Search string
  116. }
  117. type MySQL struct {
  118. Show *sql.Config
  119. Manager *sql.Config
  120. }
  121. type Redis struct {
  122. Feed *struct {
  123. *redis.Config
  124. ExpireRecommend xtime.Duration
  125. ExpireBlack xtime.Duration
  126. }
  127. Upper *struct {
  128. *redis.Config
  129. ExpireUpper xtime.Duration
  130. }
  131. }
  132. type Memcache struct {
  133. Feed *struct {
  134. *memcache.Config
  135. ExpireArchive xtime.Duration
  136. }
  137. Cache *struct {
  138. *memcache.Config
  139. ExpireCache xtime.Duration
  140. }
  141. }
  142. type Feed struct {
  143. // feed
  144. FeedCacheCount int
  145. LiveFeedCount int
  146. // index
  147. Index *Index
  148. // ad
  149. CMResource map[string]int64
  150. }
  151. type Index struct {
  152. Count int
  153. IPadCount int
  154. MoePosition int
  155. FollowPosition int
  156. // only archive for data disaster recovery
  157. Abnormal bool
  158. Interest []string
  159. FollowMode *feed.FollowMode
  160. }
  161. func init() {
  162. flag.StringVar(&confPath, "conf", "", "default config path")
  163. }
  164. // Init init conf
  165. func Init() error {
  166. if confPath != "" {
  167. return local()
  168. }
  169. return remote()
  170. }
  171. func local() (err error) {
  172. _, err = toml.DecodeFile(confPath, &Conf)
  173. return
  174. }
  175. func remote() (err error) {
  176. if client, err = conf.New(); err != nil {
  177. return
  178. }
  179. if err = load(); err != nil {
  180. return
  181. }
  182. client.Watch("app-feed.toml")
  183. go func() {
  184. for range client.Event() {
  185. log.Info("config reload")
  186. if load() != nil {
  187. log.Error("config reload error (%v)", err)
  188. }
  189. }
  190. }()
  191. return
  192. }
  193. func load() (err error) {
  194. var (
  195. s string
  196. ok bool
  197. tmpConf *Config
  198. )
  199. if s, ok = client.Toml2(); !ok {
  200. return errors.New("load config center error")
  201. }
  202. if _, err = toml.Decode(s, &tmpConf); err != nil {
  203. return errors.New("could not decode config")
  204. }
  205. *Conf = *tmpConf
  206. return
  207. }