conf.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/antispam"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. "go-common/app/service/main/videoup/model/archive"
  18. "go-common/library/net/rpc/warden"
  19. )
  20. var (
  21. confPath string
  22. // Conf is global config
  23. Conf = &Config{}
  24. client *conf.Client
  25. )
  26. // Config service config
  27. type Config struct {
  28. // base
  29. // host
  30. Host *Host
  31. // channal len
  32. ChanSize int64
  33. // ecode
  34. Ecode *ecode.Config
  35. // Xlog
  36. Xlog *log.Config
  37. // tracer
  38. Tracer *trace.Config
  39. // tick load pgc
  40. Tick time.Duration
  41. // db
  42. DB *DB
  43. // databus
  44. VideoupPub *databus.Config
  45. VideoupPGCPub *databus.Config
  46. // redis
  47. Redis *Redis
  48. Memcache *Memcache
  49. // http client test
  50. HTTPClient *HTTPClient
  51. // keep Aids
  52. KeepArc *KeepArc
  53. // DmVerifyKey dm_key
  54. DmVerifyKey string
  55. // Monitor
  56. Monitor *Monitor
  57. // PubAgent
  58. PubAgent *PubAgent
  59. // AsyncThreshold
  60. AsyncThreshold int
  61. SplitThreshold int
  62. SplitGroupCount int
  63. FailThreshold int
  64. EditTimeout time.Duration
  65. GrayGroup int
  66. //BM bladermaster config
  67. BM *bm.ServerConfig
  68. //AntispamConf limit request
  69. AntispamConf *antispam.Config
  70. Property *Property
  71. // rpc
  72. AccRPC *warden.ClientConfig
  73. }
  74. // Property .
  75. type Property struct {
  76. MSG []*archive.MSG
  77. }
  78. // Memcache conf.
  79. type Memcache struct {
  80. Archive struct {
  81. *memcache.Config
  82. TplExpire time.Duration
  83. }
  84. }
  85. // Monitor define sms monitor conf
  86. type Monitor struct {
  87. Tels string
  88. Env string
  89. Count int64
  90. }
  91. // PubAgent struct
  92. type PubAgent struct {
  93. Proxy string
  94. PGCSubmit string
  95. PGCDRMSubmit string
  96. UGCSubmit string
  97. UGCFirstRound string
  98. }
  99. // Host define host info
  100. type Host struct {
  101. Mission string
  102. Account string
  103. Monitor string
  104. APICO string
  105. MSG string
  106. Manager string
  107. }
  108. // DB define MySQL config
  109. type DB struct {
  110. Archive *sql.Config
  111. ArchiveRead *sql.Config
  112. ArchiveSlave *sql.Config
  113. CreativeCenter *sql.Config
  114. Dede *sql.Config
  115. Manager *sql.Config
  116. Oversea *sql.Config
  117. }
  118. // Redis define redis config
  119. type Redis struct {
  120. Track *TrackRedis
  121. }
  122. // TrackRedis define track redis config
  123. type TrackRedis struct {
  124. *redis.Config
  125. Expire time.Duration
  126. }
  127. // HTTPClient test
  128. type HTTPClient struct {
  129. Read *bm.ClientConfig
  130. Write *bm.ClientConfig
  131. }
  132. // KeepArc keep archive to mid
  133. type KeepArc struct {
  134. Aids []int64
  135. Mid int64
  136. }
  137. func init() {
  138. flag.StringVar(&confPath, "conf", "", "default config path")
  139. }
  140. // Init init conf
  141. func Init() (err error) {
  142. if confPath != "" {
  143. _, err = toml.DecodeFile(confPath, &Conf)
  144. return
  145. }
  146. err = remote()
  147. return
  148. }
  149. func remote() (err error) {
  150. if client, err = conf.New(); err != nil {
  151. return
  152. }
  153. if err = load(); err != nil {
  154. return
  155. }
  156. go func() {
  157. for range client.Event() {
  158. log.Info("config reload")
  159. if load() != nil {
  160. log.Error("config reload error (%v)", err)
  161. }
  162. }
  163. }()
  164. return
  165. }
  166. func load() (err error) {
  167. var (
  168. s string
  169. ok bool
  170. tmpConf *Config
  171. )
  172. if s, ok = client.Toml2(); !ok {
  173. return errors.New("load config center error")
  174. }
  175. if _, err = toml.Decode(s, &tmpConf); err != nil {
  176. return errors.New("could not decode config")
  177. }
  178. *Conf = *tmpConf
  179. return
  180. }