conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/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. "github.com/BurntSushi/toml"
  18. )
  19. // config
  20. var (
  21. confPath string
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config struct
  26. type Config struct {
  27. // base
  28. Tick time.Duration
  29. Videoshot *Videoshot
  30. // xlog
  31. Xlog *log.Config
  32. // tracer
  33. Tracer *trace.Config
  34. // http
  35. BM *BM
  36. // http client
  37. PlayerClient *bm.ClientConfig
  38. // switch get player
  39. PlayerSwitch bool
  40. PlayerNum int64
  41. // player qn config
  42. PlayerQn []int
  43. PlayerVipQn []int
  44. BnjList []int64
  45. // PlayerAPI path
  46. PlayerAPI string
  47. PGCPlayerAPI string
  48. RPCServer *rpc.ServerConfig
  49. // db
  50. DB *DB
  51. // ecode
  52. Ecode *ecode.Config
  53. // rpc client
  54. AccountRPC *rpc.ClientConfig
  55. // grpc client
  56. AccClient *warden.ClientConfig
  57. // mc
  58. Memcache *Memcache
  59. // redis
  60. Redis *Redis
  61. // databus
  62. Databus *databus.Config
  63. StatDatabus *databus.Config
  64. ShareDatabus *databus.Config
  65. CacheDatabus *databus.Config
  66. }
  67. // BM http
  68. type BM struct {
  69. Inner *bm.ServerConfig
  70. Local *bm.ServerConfig
  71. }
  72. // Videoshot videoshot uri and key
  73. type Videoshot struct {
  74. URI string
  75. Key string
  76. }
  77. // DB db config
  78. type DB struct {
  79. // archive db
  80. Arc *sql.Config
  81. ArcRead *sql.Config
  82. ArcResult *sql.Config
  83. Stat *sql.Config
  84. Click *sql.Config
  85. }
  86. // Memcache memcache config
  87. type Memcache struct {
  88. Archive *struct {
  89. *memcache.Config
  90. ArchiveExpire time.Duration
  91. VideoExpire time.Duration
  92. StatExpire time.Duration
  93. }
  94. }
  95. // Redis redis config
  96. type Redis struct {
  97. Archive *struct {
  98. *redis.Config
  99. }
  100. }
  101. func init() {
  102. flag.StringVar(&confPath, "conf", "", "default config path")
  103. }
  104. // Init init config.
  105. func Init() (err error) {
  106. if confPath != "" {
  107. _, err = toml.DecodeFile(confPath, &Conf)
  108. return
  109. }
  110. err = remote()
  111. return
  112. }
  113. func remote() (err error) {
  114. if client, err = conf.New(); err != nil {
  115. return
  116. }
  117. if err = load(); err != nil {
  118. return
  119. }
  120. client.Watch("archive-service.toml")
  121. go func() {
  122. for range client.Event() {
  123. log.Info("config reload")
  124. if load() != nil {
  125. log.Error("config reload error (%v)", err)
  126. }
  127. }
  128. }()
  129. return
  130. }
  131. func load() (err error) {
  132. var (
  133. s string
  134. ok bool
  135. tmpConf = &Config{}
  136. )
  137. if s, ok = client.Toml2(); !ok {
  138. return errors.New("load config center error")
  139. }
  140. if _, err = toml.Decode(s, tmpConf); err != nil {
  141. return errors.New("could not decode config")
  142. }
  143. *Conf = *tmpConf
  144. return
  145. }