conf.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/elastic"
  10. "go-common/library/database/sql"
  11. ecode "go-common/library/ecode/tip"
  12. "go-common/library/log"
  13. "go-common/library/log/infoc"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/http/blademaster/middleware/antispam"
  16. "go-common/library/net/http/blademaster/middleware/auth"
  17. "go-common/library/net/http/blademaster/middleware/verify"
  18. "go-common/library/net/rpc"
  19. "go-common/library/net/rpc/warden"
  20. "go-common/library/net/trace"
  21. "go-common/library/queue/databus"
  22. "go-common/library/time"
  23. "github.com/BurntSushi/toml"
  24. )
  25. var (
  26. confPath string
  27. client *conf.Client
  28. // Conf config export var
  29. Conf = &Config{}
  30. )
  31. // Config dm config struct
  32. type Config struct {
  33. // ecode
  34. Ecode *ecode.Config
  35. // log
  36. Xlog *log.Config
  37. Infoc2 *infoc.Config
  38. // tracer
  39. Tracer *trace.Config
  40. Auth *auth.Config
  41. Verify *verify.Config
  42. // database
  43. DB *DB
  44. // redis
  45. Redis *Redis
  46. // memcache
  47. Memcache *Memcache
  48. // localcache
  49. Localcache *Localcache
  50. // host
  51. Host *Host
  52. // http
  53. HTTPServer *bm.ServerConfig
  54. // http client
  55. HTTPCli *bm.ClientConfig
  56. // rpc client
  57. ArchiveRPC *rpc.ClientConfig
  58. // account rpc client
  59. AccountRPC *warden.ClientConfig
  60. // assist rpc client
  61. AssistRPC *rpc.ClientConfig
  62. //coin rpc client
  63. CoinRPC *rpc.ClientConfig
  64. // thumbup rpc client
  65. ThumbupRPC *warden.ClientConfig
  66. // relation rpc client
  67. RelationRPC *rpc.ClientConfig
  68. // seq-server rpc client
  69. SeqRPC *rpc.ClientConfig
  70. Seq *Seq
  71. // Location rpc client
  72. LocationRPC *rpc.ClientConfig
  73. // MemberRpc Rpc
  74. MemberRPC *rpc.ClientConfig
  75. // Filter Rpc
  76. FilterRPC *warden.ClientConfig
  77. // Figure RPC
  78. FigureRPC *rpc.ClientConfig
  79. // UgcPayRPC RPC
  80. UgcPayRPC *warden.ClientConfig
  81. // SpyRPC RPC
  82. SpyRPC *rpc.ClientConfig
  83. // Season RPC
  84. SeasonRPC *warden.ClientConfig
  85. // databus
  86. Databus *databus.Config
  87. ActionPub *databus.Config
  88. // dm list realname
  89. Realname *Realname
  90. // dm rpc server
  91. RPCServer *rpc.ServerConfig
  92. // Antispam
  93. Antispam *antispam.Config
  94. // supervision conf
  95. Supervision *Supervision
  96. // Elastic config
  97. Elastic *elastic.Config
  98. // Bfs
  99. Bfs *Bfs
  100. // Subtitle Check Databus
  101. SubtitleCheckPub *databus.Config
  102. // Garbage danmu Switch
  103. Switch *Switch
  104. // BroadcastLimit
  105. BroadcastLimit *BroadcastLimit
  106. // DmFlag
  107. DmFlag *DmFlag
  108. }
  109. // DmFlag .
  110. type DmFlag struct {
  111. RecFlag int
  112. RecText string
  113. RecSwitch int
  114. }
  115. // BroadcastLimit broadcast limit
  116. type BroadcastLimit struct {
  117. Limit int
  118. Interval int
  119. }
  120. // Switch .
  121. type Switch struct {
  122. GarbageDanmu bool
  123. }
  124. // Bfs .
  125. type Bfs struct {
  126. Client *bfs.Config
  127. BucketSubtitle string
  128. }
  129. //Seq Conf
  130. type Seq struct {
  131. DM *struct {
  132. BusinessID int64
  133. Token string
  134. }
  135. Subtitle *struct {
  136. BusinessID int64
  137. Token string
  138. }
  139. }
  140. // DB mysql
  141. type DB struct {
  142. DMReader *sql.Config
  143. DMWriter *sql.Config
  144. DM *sql.Config
  145. }
  146. // Redis dm redis
  147. type Redis struct {
  148. DM *struct {
  149. *redis.Config
  150. Expire time.Duration
  151. }
  152. DMRct *struct {
  153. *redis.Config
  154. Expire time.Duration
  155. }
  156. DMSeg *struct {
  157. *redis.Config
  158. Expire time.Duration
  159. }
  160. }
  161. // Memcache dm memcache
  162. type Memcache struct {
  163. DM *struct {
  164. *memcache.Config
  165. DMExpire time.Duration
  166. SubjectExpire time.Duration
  167. HistoryExpire time.Duration
  168. AjaxExpire time.Duration
  169. DMMaskExpire time.Duration
  170. }
  171. Filter *struct {
  172. *memcache.Config
  173. Expire time.Duration
  174. }
  175. Subtitle *struct {
  176. *memcache.Config
  177. Expire time.Duration
  178. }
  179. DMSeg *struct {
  180. *memcache.Config
  181. DMExpire time.Duration
  182. DMLimiterExpire time.Duration
  183. }
  184. }
  185. // Localcache cache stored in local
  186. type Localcache struct {
  187. Oids []int64
  188. Expire time.Duration
  189. ViewAids []int64
  190. ViewExpire time.Duration
  191. }
  192. // Realname realname switch and config
  193. type Realname struct {
  194. SwitchOn bool
  195. Whitelist []int64
  196. Threshold map[string]int64
  197. }
  198. // Host http host
  199. type Host struct {
  200. AI string
  201. API string
  202. Archive string
  203. Message string
  204. Search string
  205. MaskCloud string
  206. Advert string
  207. Upos string
  208. Self string
  209. }
  210. // Supervision supervision .
  211. type Supervision struct {
  212. Completed bool
  213. StartTime string
  214. EndTime string
  215. Location string
  216. }
  217. func init() {
  218. flag.StringVar(&confPath, "conf", "", "config path")
  219. }
  220. //Init int config
  221. func Init() error {
  222. if confPath != "" {
  223. return local()
  224. }
  225. return remote()
  226. }
  227. func local() (err error) {
  228. _, err = toml.DecodeFile(confPath, &Conf)
  229. return
  230. }
  231. func remote() (err error) {
  232. if client, err = conf.New(); err != nil {
  233. return
  234. }
  235. if err = load(); err != nil {
  236. return
  237. }
  238. go func() {
  239. for range client.Event() {
  240. log.Info("config reload")
  241. if load() != nil {
  242. log.Error("config reload error (%v)", err)
  243. }
  244. }
  245. }()
  246. return
  247. }
  248. func load() (err error) {
  249. var (
  250. s string
  251. ok bool
  252. tmpConf *Config
  253. )
  254. if s, ok = client.Toml2(); !ok {
  255. return errors.New("load config center error")
  256. }
  257. if _, err = toml.Decode(s, &tmpConf); err != nil {
  258. return errors.New("could not decode config")
  259. }
  260. *Conf = *tmpConf
  261. return
  262. }