conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // CommonConfig .
  23. type CommonConfig struct {
  24. ExpireTime int64
  25. }
  26. // LRUConfig .
  27. type LRUConfig struct {
  28. InstCnt int
  29. Bucket int
  30. Capacity int
  31. Timeout int64
  32. }
  33. type AreaInfo struct {
  34. Name string
  35. }
  36. // Config .
  37. type Config struct {
  38. Log *log.Config
  39. BM *bm.ServerConfig
  40. Verify *verify.Config
  41. Tracer *trace.Config
  42. Redis *redis.Config
  43. MySQL *sql.Config
  44. LiveAppMySQL *sql.Config
  45. Ecode *ecode.Config
  46. LiveDanmuSub *databus.Config //发送弹幕回调配置
  47. LiveGiftSendByPaySub *databus.Config
  48. LiveGiftSendByFreeSub *databus.Config
  49. LiveGuardBuySub *databus.Config
  50. LivePopularitySub *databus.Config
  51. LiveValidLiveDaysSub *databus.Config
  52. LiveRoomTagSub *databus.Config
  53. LiveRankListSub *databus.Config
  54. Common *CommonConfig
  55. LRUCache *LRUConfig
  56. FirstAreas map[string]*AreaInfo
  57. }
  58. func init() {
  59. flag.StringVar(&confPath, "conf", "", "default config path")
  60. }
  61. // Init init conf
  62. func Init() error {
  63. if confPath != "" {
  64. return local()
  65. }
  66. return remote()
  67. }
  68. func local() (err error) {
  69. _, err = toml.DecodeFile(confPath, &Conf)
  70. return
  71. }
  72. func remote() (err error) {
  73. if client, err = conf.New(); err != nil {
  74. return
  75. }
  76. if err = load(); err != nil {
  77. return
  78. }
  79. go func() {
  80. for range client.Event() {
  81. log.Info("config reload")
  82. if load() != nil {
  83. log.Error("config reload error (%v)", err)
  84. }
  85. }
  86. }()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Toml2(); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }