conf.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. "go-common/library/log/infoc"
  12. "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/auth"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/trace"
  18. "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. )
  21. // global var
  22. var (
  23. confPath string
  24. client *conf.Client
  25. // Conf config
  26. Conf = &Config{}
  27. )
  28. // Config config set
  29. type Config struct {
  30. // log
  31. Log *log.Config
  32. // ecode
  33. Ecode *ecode.Config
  34. // http client
  35. HTTPClient *httpClient
  36. // HTTPServer
  37. HTTPServer *blademaster.ServerConfig
  38. // tracer
  39. Tracer *trace.Config
  40. // auth
  41. Auth *auth.Config
  42. // verify
  43. Verify *verify.Config
  44. // CouponRPC
  45. CouponRPC *rpc.ClientConfig
  46. // ArchiveRPC
  47. ArchiveRPC *rpc.ClientConfig
  48. // DynamicRPC
  49. DynamicRPC *rpc.ClientConfig
  50. // LocationRPC
  51. LocationRPC *rpc.ClientConfig
  52. // CoinRPC
  53. CoinRPC *rpc.ClientConfig
  54. // TagRPC
  55. TagRPC *rpc.ClientConfig
  56. // ArticleRPC
  57. ArticleRPC *rpc.ClientConfig
  58. // ResourceRPC
  59. ResourceRPC *rpc.ClientConfig
  60. // RelationRPC
  61. RelationRPC *rpc.ClientConfig
  62. // ThumbupRPC
  63. ThumbupRPC *rpc.ClientConfig
  64. // DM2RPC
  65. Dm2RPC *rpc.ClientConfig
  66. // FavRPC
  67. FavRPC *rpc.ClientConfig
  68. // Host
  69. Host host
  70. // redis
  71. Redis *redisConf
  72. // degrade
  73. DegradeConfig *degradeConfig
  74. // WEB
  75. WEB *web
  76. // Tag
  77. Tag *tag
  78. // DefaultTop
  79. DefaultTop *defaultTop
  80. // Bfs
  81. Bfs *bfs
  82. // Infoc2
  83. Infoc2 *infoc.Config
  84. // Rule
  85. Rule *rule
  86. // Warden Client
  87. BroadcastClient *warden.ClientConfig
  88. CoinClient *warden.ClientConfig
  89. ArcClient *warden.ClientConfig
  90. AccClient *warden.ClientConfig
  91. ShareClient *warden.ClientConfig
  92. UGCClient *warden.ClientConfig
  93. // bnj
  94. Bnj2019 *bnj2019
  95. }
  96. type rule struct {
  97. // min cache rank count
  98. MinRankCount int
  99. // min cache rank index count
  100. MinRankIndexCount int
  101. // min cache rank region count
  102. MinRankRegionCount int
  103. // min cache rank recommend count
  104. MinRankRecCount int
  105. // min cache rank tag count
  106. MinRankTagCount int
  107. // min cache dynamic count
  108. MinDyCount int
  109. // min newlist tid arc count
  110. MinNewListCnt int
  111. // Elec
  112. ElecShowTypeIDs []int32
  113. // AuthorRecCnt author recommend count
  114. AuthorRecCnt int
  115. // RelatedArcCnt related archive limit count
  116. RelatedArcCnt int
  117. // MaxHelpPageSize help detail search max page count
  118. MaxHelpPageSize int
  119. // newlist
  120. MaxArcsPageSize int
  121. // max size of second region newlist.
  122. MaxSecondCacheSize int
  123. // max size of first region newlist.
  124. MaxFirstCacheSize int
  125. // default num of dynamic archives
  126. DynamicNumArcs int
  127. // regions count
  128. RegionsCount int
  129. // bangumi count
  130. BangumiCount int
  131. // MaxArtPageSize max article page size
  132. MaxArtPageSize int
  133. // article up list get count
  134. ArtUpListGetCnt int
  135. // article up list count
  136. ArtUpListCnt int
  137. // min wechat hot count
  138. MinWxHotCount int
  139. // Rids first region ids
  140. Rids []int32
  141. // no related aids
  142. NoRelAids []int64
  143. }
  144. type httpClient struct {
  145. Read *blademaster.ClientConfig
  146. Write *blademaster.ClientConfig
  147. BigData *blademaster.ClientConfig
  148. Help *blademaster.ClientConfig
  149. Search *blademaster.ClientConfig
  150. Pay *blademaster.ClientConfig
  151. }
  152. type host struct {
  153. Rank string
  154. API string
  155. Data string
  156. Space string
  157. Elec string
  158. ArcAPI string
  159. LiveAPI string
  160. HelpAPI string
  161. Mall string
  162. Search string
  163. Manager string
  164. Pay string
  165. AbServer string
  166. }
  167. type tag struct {
  168. PageSize int
  169. MaxSize int
  170. }
  171. type redisConf struct {
  172. LocalRedis *localRedis
  173. BakRedis *bakRedis
  174. }
  175. type localRedis struct {
  176. *redis.Config
  177. RankingExpire time.Duration
  178. NewlistExpire time.Duration
  179. RcExpire time.Duration
  180. IndexIconExpire time.Duration
  181. WxHotExpire time.Duration
  182. }
  183. type bakRedis struct {
  184. *redis.Config
  185. RankingExpire time.Duration
  186. NewlistExpire time.Duration
  187. RegionExpire time.Duration
  188. ArchiveExpire time.Duration
  189. TagExpire time.Duration
  190. CardExpire time.Duration
  191. RcExpire time.Duration
  192. ArtUpExpire time.Duration
  193. IndexIconExpire time.Duration
  194. HelpExpire time.Duration
  195. OlListExpire time.Duration
  196. WxHotExpire time.Duration
  197. AppealLimitExpire time.Duration
  198. }
  199. type web struct {
  200. PullRegionInterval time.Duration
  201. PullOnlineInterval time.Duration
  202. PullIndexIconInterval time.Duration
  203. SearchEggInterval time.Duration
  204. OnlineCount int
  205. SpecailInterval time.Duration
  206. }
  207. type defaultTop struct {
  208. SImg string
  209. LImg string
  210. }
  211. type bfs struct {
  212. Addr string
  213. Bucket string
  214. Key string
  215. Secret string
  216. MaxFileSize int
  217. Timeout time.Duration
  218. }
  219. type degradeConfig struct {
  220. Expire int32
  221. Memcache *memcache.Config
  222. }
  223. type bnj2019 struct {
  224. Open bool
  225. LiveAid int64
  226. BnjMainAid int64
  227. FakeElec int64
  228. BnjListAids []int64
  229. BnjTick time.Duration
  230. Timeline []*struct {
  231. Name string
  232. Start xtime.Time
  233. End xtime.Time
  234. Cover string
  235. H5Cover string
  236. }
  237. }
  238. func init() {
  239. flag.StringVar(&confPath, "conf", "", "default config path")
  240. }
  241. // Init init conf
  242. func Init() error {
  243. if confPath != "" {
  244. return local()
  245. }
  246. return remote()
  247. }
  248. func local() (err error) {
  249. _, err = toml.DecodeFile(confPath, &Conf)
  250. return
  251. }
  252. func remote() (err error) {
  253. if client, err = conf.New(); err != nil {
  254. return
  255. }
  256. if err = load(); err != nil {
  257. return
  258. }
  259. client.Watch("web-interface.toml")
  260. go func() {
  261. for range client.Event() {
  262. log.Info("config reload")
  263. if load() != nil {
  264. log.Error("config reload error (%v)", err)
  265. }
  266. }
  267. }()
  268. return
  269. }
  270. func load() (err error) {
  271. var (
  272. s string
  273. ok bool
  274. tmpConf *Config
  275. )
  276. if s, ok = client.Toml2(); !ok {
  277. return errors.New("load config center error")
  278. }
  279. if _, err = toml.Decode(s, &tmpConf); err != nil {
  280. return errors.New("could not decode config")
  281. }
  282. *Conf = *tmpConf
  283. return
  284. }