conf.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. "go-common/library/queue/databus"
  17. xtime "go-common/library/time"
  18. "github.com/BurntSushi/toml"
  19. )
  20. // conf init.
  21. var (
  22. confPath string
  23. client *conf.Client
  24. // Conf config
  25. Conf = &Config{}
  26. )
  27. // Config struct
  28. type Config struct {
  29. // Local cache
  30. LocalCache bool
  31. // interface XLog
  32. XLog *log.Config
  33. // tick time
  34. Tick xtime.Duration
  35. // tracer
  36. Tracer *trace.Config
  37. // databus
  38. UseractPub *databus.Config
  39. // httpClinet
  40. HTTPClient *bm.ClientConfig
  41. // httpIm9
  42. HTTPIm9 *bm.ClientConfig
  43. // httpSearch
  44. HTTPSearch *bm.ClientConfig
  45. // httpWrite
  46. HTTPWrite *bm.ClientConfig
  47. // httpLive
  48. HTTPLive *bm.ClientConfig
  49. // httpbangumi
  50. HTTPBangumi *bm.ClientConfig
  51. // httpbplus
  52. HTTPBPlus *bm.ClientConfig
  53. // httpgame
  54. HTTPGame *bm.ClientConfig
  55. // http
  56. BM *HTTPServers
  57. // host
  58. Host *Host
  59. // rpc client
  60. AccountRPC *rpc.ClientConfig
  61. ArchiveRPC *rpc.ClientConfig
  62. TagRPC *rpc.ClientConfig
  63. ArticleRPC *rpc.ClientConfig
  64. RelationRPC *rpc.ClientConfig
  65. ThumbupRPC *rpc.ClientConfig
  66. HistoryRPC *rpc.ClientConfig
  67. ResourceRPC *rpc.ClientConfig
  68. MemberRPC *rpc.ClientConfig
  69. LocationRPC *rpc.ClientConfig
  70. // db
  71. MySQL *MySQL
  72. // ecode
  73. // ecode
  74. Ecode *ecode.Config
  75. // redis
  76. Redis *Redis
  77. // mc
  78. Memcache *Memcache
  79. // search
  80. Search *Search
  81. // space
  82. Space *Space
  83. // contribute
  84. ContributePub *databus.Config
  85. CoinClient *warden.ClientConfig
  86. // BroadcastRPC grpc
  87. PGCRPC *warden.ClientConfig
  88. // build limit
  89. SearchBuildLimit *SearchBuildLimit
  90. // login build
  91. LoginBuild *LoginBuild
  92. // infoc
  93. Infoc *infoc.Config
  94. // fav Client
  95. FavClient *warden.ClientConfig
  96. // search dynamic
  97. SearchDynamicSwitch *SearchDynamicSwitch
  98. }
  99. // LoginBuild is
  100. type LoginBuild struct {
  101. Iphone int
  102. }
  103. // HTTPServers Http Servers
  104. type HTTPServers struct {
  105. Outer *bm.ServerConfig
  106. }
  107. // Host struct
  108. type Host struct {
  109. Account string
  110. Bangumi string
  111. APICo string
  112. Im9 string
  113. Search string
  114. Game string
  115. Space string
  116. Elec string
  117. VC string
  118. APILiveCo string
  119. WWW string
  120. Show string
  121. Pay string
  122. Member string
  123. Mall string
  124. }
  125. // MySQL struct
  126. type MySQL struct {
  127. Show *sql.Config
  128. }
  129. // Redis struct
  130. type Redis struct {
  131. Contribute *struct {
  132. *redis.Config
  133. Expire xtime.Duration
  134. }
  135. }
  136. // Memcache struct
  137. type Memcache struct {
  138. Archive *struct {
  139. *memcache.Config
  140. RecommedExpire xtime.Duration
  141. ArchiveExpire xtime.Duration
  142. }
  143. }
  144. // Search struct
  145. type Search struct {
  146. SeasonNum int
  147. MovieNum int
  148. SeasonMore int
  149. MovieMore int
  150. UpUserNum int
  151. UVLimit int
  152. UserNum int
  153. UserVideoLimit int
  154. BiliUserNum int
  155. BiliUserVideoLimit int
  156. OperationNum int
  157. IPadSearchBangumi int
  158. IPadSearchFt int
  159. }
  160. // Space struct
  161. type Space struct {
  162. ForbidMid []int64
  163. }
  164. // SearchBuildLimit struct
  165. type SearchBuildLimit struct {
  166. PGCHighLightIOS int
  167. PGCHighLightAndroid int
  168. PGCALLIOS int
  169. PGCALLAndroid int
  170. SpecialerGuideIOS int
  171. SpecialerGuideAndroid int
  172. SearchArticleIOS int
  173. SearchArticleAndroid int
  174. ComicIOS int
  175. ComicAndroid int
  176. ChannelIOS int
  177. ChannelAndroid int
  178. CooperationIOS int
  179. CooperationAndroid int
  180. QueryCorIOS int
  181. QueryCorAndroid int
  182. SugDetailIOS int
  183. SugDetailAndroid int
  184. NewTwitterIOS int
  185. NewTwitterAndroid int
  186. }
  187. type SearchDynamicSwitch struct {
  188. IsUP bool
  189. IsCount bool
  190. }
  191. func init() {
  192. flag.StringVar(&confPath, "conf", "", "config path")
  193. }
  194. // Init init conf
  195. func Init() error {
  196. if confPath != "" {
  197. return local()
  198. }
  199. return remote()
  200. }
  201. func local() (err error) {
  202. _, err = toml.DecodeFile(confPath, &Conf)
  203. return
  204. }
  205. func remote() (err error) {
  206. if client, err = conf.New(); err != nil {
  207. return
  208. }
  209. if err = load(); err != nil {
  210. return
  211. }
  212. client.Watch("app-interface.toml")
  213. go func() {
  214. for range client.Event() {
  215. log.Info("config reload")
  216. if load() != nil {
  217. log.Error("config reload error (%v)", err)
  218. }
  219. }
  220. }()
  221. return
  222. }
  223. func load() (err error) {
  224. var (
  225. s string
  226. ok bool
  227. tmpConf *Config
  228. )
  229. if s, ok = client.Toml2(); !ok {
  230. return errors.New("load config center error")
  231. }
  232. if _, err = toml.Decode(s, &tmpConf); err != nil {
  233. return errors.New("could not decode config")
  234. }
  235. *Conf = *tmpConf
  236. return
  237. }