conf.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. // confPath is.
  21. confPath string
  22. // Conf is.
  23. Conf = &Config{}
  24. // client is.
  25. client *conf.Client
  26. )
  27. // Config struct
  28. type Config struct {
  29. ShowInfoc *infoc.Config
  30. TagInfoc *infoc.Config
  31. RedirectInfoc *infoc.Config
  32. CoinInfoc *infoc.Config
  33. ViewInfoc *infoc.Config
  34. RelateInfoc *infoc.Config
  35. // show XLog
  36. XLog *log.Config
  37. // tick time
  38. Tick xtime.Duration
  39. // tracer
  40. Tracer *trace.Config
  41. // httpClinet
  42. HTTPClient *bm.ClientConfig
  43. // httpAsyn
  44. HTTPClientAsyn *bm.ClientConfig
  45. // httpData
  46. HTTPData *bm.ClientConfig
  47. // httpTag
  48. HTTPTag *bm.ClientConfig
  49. // httpBangumi
  50. HTTPBangumi *bm.ClientConfig
  51. // HTTPSearch
  52. HTTPSearch *bm.ClientConfig
  53. // HTTPAudio
  54. HTTPAudio *bm.ClientConfig
  55. // HTTPWrite
  56. HTTPWrite *bm.ClientConfig
  57. // http
  58. BM *HTTPServers
  59. // host
  60. Host *Host
  61. // db
  62. MySQL *MySQL
  63. // redis
  64. Redis *Redis
  65. // mc
  66. Memcache *Memcache
  67. // rpc client
  68. AccountRPC *rpc.ClientConfig
  69. RelationRPC *rpc.ClientConfig
  70. ArchiveRPC *rpc.ClientConfig
  71. ArchiveRPC2 *rpc.ClientConfig
  72. TagRPC *rpc.ClientConfig
  73. FavoriteRPC *rpc.ClientConfig
  74. CoinRPC *rpc.ClientConfig
  75. AssistRPC *rpc.ClientConfig
  76. ThumbupRPC *rpc.ClientConfig
  77. DMRPC *rpc.ClientConfig
  78. ResourceRPC *rpc.ClientConfig
  79. HisRPC *rpc.ClientConfig
  80. ArticleRPC *rpc.ClientConfig
  81. LocationRPC *rpc.ClientConfig
  82. // BroadcastRPC grpc
  83. PGCRPC *warden.ClientConfig
  84. UGCpayClient *warden.ClientConfig
  85. // ecode
  86. Ecode *ecode.Config
  87. // feed
  88. Feed *Feed
  89. // view
  90. View *View
  91. // search
  92. Search *Search
  93. }
  94. // HTTPServers Http Servers
  95. type HTTPServers struct {
  96. Outer *bm.ServerConfig
  97. }
  98. // Host struct
  99. type Host struct {
  100. Bangumi string
  101. Data string
  102. Hetongzi string
  103. APICo string
  104. Rank string
  105. BigData string
  106. Search string
  107. AI string
  108. Bvcvod string
  109. VIP string
  110. Playurl string
  111. PlayurlBk string
  112. }
  113. // MySQL struct
  114. type MySQL struct {
  115. Show *sql.Config
  116. Manager *sql.Config
  117. }
  118. // Redis struct
  119. type Redis struct {
  120. Feed *struct {
  121. *redis.Config
  122. ExpireRecommend xtime.Duration
  123. ExpireBlack xtime.Duration
  124. }
  125. }
  126. // Memcache struct
  127. type Memcache struct {
  128. Feed *struct {
  129. *memcache.Config
  130. Expire xtime.Duration
  131. }
  132. Cache *struct {
  133. *memcache.Config
  134. Expire xtime.Duration
  135. }
  136. Archive *struct {
  137. *memcache.Config
  138. RelateExpire xtime.Duration
  139. }
  140. }
  141. // Feed struct
  142. type Feed struct {
  143. // index
  144. Index *Index
  145. // ad
  146. CMResource map[string]int64
  147. }
  148. // Index struct
  149. type Index struct {
  150. Count int
  151. IPadCount int
  152. MoePosition int
  153. FollowPosition int
  154. // only archive for data disaster recovery
  155. Abnormal bool
  156. }
  157. // View struct
  158. type View struct {
  159. VipTick xtime.Duration
  160. // 相关推荐秒开个数
  161. RelateCnt int
  162. }
  163. // Search struct
  164. type Search struct {
  165. SeasonNum int
  166. MovieNum int
  167. SeasonMore int
  168. MovieMore int
  169. UpUserNum int
  170. UVLimit int
  171. UserNum int
  172. UserVideoLimit int
  173. BiliUserNum int
  174. BiliUserVideoLimit int
  175. OperationNum int
  176. IPadSearchBangumi int
  177. IPadSearchFt int
  178. }
  179. // init is.
  180. func init() {
  181. flag.StringVar(&confPath, "conf", "", "default config path")
  182. }
  183. // Init init conf
  184. func Init() error {
  185. if confPath != "" {
  186. return local()
  187. }
  188. return remote()
  189. }
  190. // local is.
  191. func local() (err error) {
  192. _, err = toml.DecodeFile(confPath, &Conf)
  193. return
  194. }
  195. // reomte is.
  196. func remote() (err error) {
  197. if client, err = conf.New(); err != nil {
  198. return
  199. }
  200. if err = load(); err != nil {
  201. return
  202. }
  203. client.Watch("app-intl.toml")
  204. go func() {
  205. for range client.Event() {
  206. log.Info("config reload")
  207. if load() != nil {
  208. log.Error("config reload error (%v)", err)
  209. }
  210. }
  211. }()
  212. return
  213. }
  214. // load is.
  215. func load() (err error) {
  216. var (
  217. s string
  218. ok bool
  219. tmpConf *Config
  220. )
  221. if s, ok = client.Toml2(); !ok {
  222. return errors.New("load config center error")
  223. }
  224. if _, err = toml.Decode(s, &tmpConf); err != nil {
  225. return errors.New("could not decode config")
  226. }
  227. *Conf = *tmpConf
  228. return
  229. }