conf.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "github.com/BurntSushi/toml"
  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. "go-common/library/time"
  17. "go-common/library/database/hbase.v2"
  18. )
  19. // Conf info.
  20. var (
  21. ConfPath string
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config struct.
  26. type Config struct {
  27. // db
  28. DB *DB
  29. // base
  30. // ecode
  31. Ecode *ecode.Config
  32. // log
  33. Log *log.Config
  34. // httpClinet
  35. HTTPClient *HTTPClient
  36. // BM
  37. BM *HTTPServers
  38. // tracer
  39. Tracer *trace.Config
  40. // host
  41. Host *Host
  42. // Databus
  43. Env string
  44. Consume bool
  45. Pub bool
  46. ArcSub *databus.Config
  47. ArcNotifySub *databus.Config
  48. UpPub *databus.Config
  49. //task
  50. TaskSub, ShareSub, RelationSub, StatLikeSub *databus.Config
  51. StatShareSub, StatCoinSub, StatFavSub *databus.Config
  52. StatReplySub, StatDMSub, StatViewSub *databus.Config
  53. NewUpSub *databus.Config
  54. Task *Task
  55. // channal len
  56. ChanSize int64
  57. //moni
  58. Monitor *Monitor
  59. // rpc client2
  60. ArchiveRPC *rpc.ClientConfig
  61. ArticleRPC *rpc.ClientConfig
  62. // grpc Client
  63. CreativeGRPClient *warden.ClientConfig
  64. UpGRPCClient *warden.ClientConfig
  65. //hot compute switch
  66. HotSwitch bool
  67. //hot compute switch
  68. HonorSwitch bool
  69. HonorStep int
  70. HonorMSGSpec string
  71. HonorFlushSpec string
  72. SendEveryWeek bool
  73. // hbase
  74. HBaseOld *HBaseConfig
  75. // infoc
  76. WeeklyHonorInfoc *infoc.Config
  77. }
  78. // DB conf.
  79. type DB struct {
  80. // Creative db
  81. Creative *sql.Config
  82. // Archive db
  83. Archive *sql.Config
  84. }
  85. // HBaseConfig for new hbase client.
  86. type HBaseConfig struct {
  87. *hbase.Config
  88. WriteTimeout time.Duration
  89. ReadTimeout time.Duration
  90. }
  91. // HTTPServers Http Servers
  92. type HTTPServers struct {
  93. Outer *bm.ServerConfig
  94. }
  95. // HTTPClient conf.
  96. type HTTPClient struct {
  97. Normal *bm.ClientConfig
  98. Slow *bm.ClientConfig
  99. }
  100. // Host conf.
  101. type Host struct {
  102. Monitor string
  103. Passport string
  104. Account string
  105. Message string
  106. API string
  107. Videoup string
  108. }
  109. // Monitor conf.
  110. type Monitor struct {
  111. Host string
  112. Moni string
  113. UserName string
  114. AppSecret string
  115. AppToken string
  116. }
  117. // Task conf.
  118. type Task struct {
  119. //扫表
  120. RowLimit int //每次从表中取的最大数据量
  121. TableJobNum int //开启n张表的扫描任务的协程数量
  122. TableConsumeNum int //开启消费表数据的协程数量
  123. //databus 消费
  124. SwitchHighQPS bool
  125. SwitchDatabus bool
  126. DatabusQueueLen int
  127. StatViewQueueLen int
  128. StatLikeQueueLen int
  129. ChanSize int
  130. //task notify
  131. SwitchMsgNotify bool
  132. TaskRowLimitNum int
  133. TaskTableJobNum int
  134. TaskTableConsumeNum int
  135. TaskExpireTime int64
  136. TaskSendHour int
  137. TaskSendMiniute int
  138. TaskSendSecond int
  139. TaskBatchMidNum int
  140. TaskMsgCode string
  141. TaskTitle string
  142. TaskContent string
  143. TestNotifyMids string
  144. //reward notify
  145. RewardRowLimitNum int
  146. RewardTableJobNum int
  147. RewardTableConsumeNum int
  148. RewardWeek int
  149. RewardLastDay int
  150. RewardLastHour int
  151. RewardLastMiniute int
  152. RewardLastSecond int
  153. RewardNowHour int
  154. RewardNowMiniute int
  155. RewardNowSecond int
  156. RewardSendHour int
  157. RewardSendMiniute int
  158. RewardSendSecond int
  159. RewardBatchMidNum int
  160. RewardMsgCode string
  161. RewardTitle string
  162. RewardContent string
  163. BiliMID int64
  164. //新手和进阶粉丝数
  165. NewFollower int64
  166. AdvancedFollower int64
  167. //单个稿件计数
  168. StatView int64
  169. StatLike int64
  170. StatReply int64
  171. StatShare int64
  172. StatFav int64
  173. StatCoin int64
  174. StatDM int64
  175. //单个稿件计数设定上限
  176. StatViewUp int64
  177. StatLikeUp int64
  178. StatReplyUp int64
  179. StatShareUp int64
  180. StatFavUp int64
  181. StatCoinUp int64
  182. StatDMUp int64
  183. }
  184. func init() {
  185. flag.StringVar(&ConfPath, "conf", "", "default config path")
  186. }
  187. // Init conf.
  188. func Init() (err error) {
  189. if ConfPath != "" {
  190. return local()
  191. }
  192. return remote()
  193. }
  194. func local() (err error) {
  195. _, err = toml.DecodeFile(ConfPath, &Conf)
  196. return
  197. }
  198. func remote() (err error) {
  199. if client, err = conf.New(); err != nil {
  200. return
  201. }
  202. if err = load(); err != nil {
  203. return
  204. }
  205. go func() {
  206. for range client.Event() {
  207. log.Info("config reload")
  208. if load() != nil {
  209. log.Error("config reload error (%v)", err)
  210. }
  211. }
  212. }()
  213. return
  214. }
  215. func load() (err error) {
  216. var (
  217. tomlStr string
  218. ok bool
  219. tmpConf *Config
  220. )
  221. if tomlStr, ok = client.Toml2(); !ok {
  222. return errors.New("load config center error")
  223. }
  224. if _, err = toml.Decode(tomlStr, &tmpConf); err != nil {
  225. return errors.New("could not decode toml config")
  226. }
  227. *Conf = *tmpConf
  228. return
  229. }