conf.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/bfs"
  9. "go-common/library/database/sql"
  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. "go-common/library/time"
  18. "github.com/BurntSushi/toml"
  19. )
  20. var (
  21. confPath string
  22. client *conf.Client
  23. // Conf export config var
  24. Conf = &Config{}
  25. )
  26. // Config danmaku config
  27. type Config struct {
  28. // base
  29. // log
  30. Xlog *log.Config
  31. Infoc2 *infoc.Config
  32. // tracer
  33. Tracer *trace.Config
  34. // http
  35. HTTPServer *bm.ServerConfig
  36. // database
  37. DB *DB
  38. // redis
  39. Redis *Redis
  40. // memcache
  41. Memcache *Memcache
  42. // Subtitle Cache
  43. SubtitleMemcache *Memcache
  44. DMMemcache *Memcache
  45. // archive rpc client
  46. ArchiveRPC *rpc.ClientConfig
  47. // seq-server rpc client
  48. SeqRPC *rpc.ClientConfig
  49. Seq *Seq
  50. // databus config
  51. Databus *Databus
  52. // dm list realname
  53. Realname map[string]int64
  54. HTTPClient *bm.ClientConfig
  55. Host *Host
  56. BFSClient *bm.ClientConfig
  57. // client
  58. FliterRPC *warden.ClientConfig
  59. // MaskCate
  60. MaskCate *MaskCate
  61. // Bfs
  62. Bfs *Bfs
  63. // cache routine size
  64. RoutineSize int
  65. // bnj
  66. BNJ *BNJ
  67. // task config
  68. TaskConf *TaskConf
  69. }
  70. // BNJ .
  71. type BNJ struct {
  72. Aid int64
  73. BnjCounter *BnjCounter
  74. BnjLiveDanmu *BnjLiveDanmu
  75. }
  76. // BnjCounter .
  77. type BnjCounter struct {
  78. SubAids []int64
  79. }
  80. // BnjLiveDanmu .
  81. type BnjLiveDanmu struct {
  82. RoomID int64
  83. Start string
  84. IgnoreRate int64
  85. Level int32
  86. IgnoreBegin time.Duration
  87. IgnoreEnd time.Duration
  88. }
  89. // BNJVideo .
  90. type BNJVideo struct {
  91. Cid int64
  92. Duration float64
  93. }
  94. // Bfs .
  95. type Bfs struct {
  96. Client *bfs.Config
  97. Dm string
  98. }
  99. //Seq Conf
  100. type Seq struct {
  101. BusinessID int64
  102. Token string
  103. }
  104. // DB mysql config struct
  105. type DB struct {
  106. DMReader *sql.Config
  107. DMWriter *sql.Config
  108. BiliDMWriter *sql.Config
  109. QueryPageSize int32
  110. }
  111. // Redis dm redis
  112. type Redis struct {
  113. DM *struct {
  114. *redis.Config
  115. Expire time.Duration
  116. }
  117. DMRct *struct {
  118. *redis.Config
  119. Expire time.Duration
  120. }
  121. DMSeg *struct {
  122. *redis.Config
  123. Expire time.Duration
  124. }
  125. }
  126. // Memcache dm memcache
  127. type Memcache struct {
  128. *memcache.Config
  129. Expire time.Duration
  130. }
  131. // Databus databus config
  132. type Databus struct {
  133. IndexCsmr *databus.Config
  134. SubjectCsmr *databus.Config
  135. ActionCsmr *databus.Config
  136. ReportCsmr *databus.Config
  137. VideoupCsmr *databus.Config
  138. SubtitleAuditCsmr *databus.Config
  139. BnjCsmr *databus.Config
  140. }
  141. // Host hosts used in dm admin
  142. type Host struct {
  143. Videoup string
  144. Mask string
  145. DataRank string
  146. MerakHost string
  147. APILive string
  148. }
  149. // MaskCate .
  150. type MaskCate struct {
  151. Tids []int64
  152. Interval time.Duration
  153. Limit int
  154. }
  155. // TaskConf .
  156. type TaskConf struct {
  157. DelInterval time.Duration
  158. ResInterval time.Duration
  159. ResFieldLen int
  160. DelNum int
  161. DelLimit int64
  162. MsgCC []string
  163. MsgPublicKey string
  164. }
  165. func init() {
  166. flag.StringVar(&confPath, "conf", "", "config path")
  167. }
  168. //Init int config
  169. func Init() error {
  170. if confPath != "" {
  171. return local()
  172. }
  173. return remote()
  174. }
  175. func local() (err error) {
  176. _, err = toml.DecodeFile(confPath, &Conf)
  177. return
  178. }
  179. func remote() (err error) {
  180. if client, err = conf.New(); err != nil {
  181. return
  182. }
  183. if err = load(); err != nil {
  184. return
  185. }
  186. go func() {
  187. for range client.Event() {
  188. log.Info("config reload")
  189. if load() != nil {
  190. log.Error("config reload error (%v)", err)
  191. }
  192. }
  193. }()
  194. return
  195. }
  196. func load() (err error) {
  197. var tmpConf *Config
  198. value, ok := client.Toml2()
  199. if !ok {
  200. return errors.New("load config center error")
  201. }
  202. if _, err = toml.Decode(value, &tmpConf); err != nil {
  203. return errors.New("could not decode config")
  204. }
  205. *Conf = *tmpConf
  206. return
  207. }