conf.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/net/rpc/warden"
  6. "go-common/library/queue/databus"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/conf"
  10. "go-common/library/database/orm"
  11. "go-common/library/database/sql"
  12. eCode "go-common/library/ecode/tip"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/http/blademaster/middleware/verify"
  16. "go-common/library/net/rpc"
  17. liverRPC "go-common/library/net/rpc/liverpc"
  18. "go-common/library/net/trace"
  19. "github.com/BurntSushi/toml"
  20. )
  21. var (
  22. confPath string
  23. // TraceInit weather need init trace
  24. TraceInit bool
  25. client *conf.Client
  26. // Conf config
  27. Conf = &Config{}
  28. )
  29. // Config .
  30. type Config struct {
  31. Log *log.Config
  32. BM *bm.ServerConfig
  33. BMClient *bm.ClientConfig
  34. Verify *verify.Config
  35. Tracer *trace.Config
  36. VipRedis *redis.Config
  37. GuardRedis *redis.Config
  38. Redis *redis.Config
  39. Memcache *memcache.Config
  40. ExpMemcache *memcache.Config
  41. LiveUserMysql *sql.Config
  42. LiveAppMySQL *sql.Config
  43. LiveAppORM *orm.Config
  44. Ecode *eCode.Config
  45. LiveVipChangePub *databus.Config
  46. UserExpMySQL *sql.Config
  47. LiveRPC map[string]*liverRPC.ClientConfig
  48. LiveEntryEffectPub *databus.Config
  49. GuardCfg *GuardCfg
  50. AccountRPC *rpc.ClientConfig
  51. Switch *ConfigSwitch
  52. UserExpExpire *UserExpExpireConf
  53. UserDaHangHaiExpire *UserDhhExpireConf
  54. // report
  55. Report *databus.Config
  56. XanchorClient *warden.ClientConfig
  57. }
  58. // GuardCfg config for guard
  59. type GuardCfg struct {
  60. OpenEntryEffectDatabus bool
  61. EnableGuardBroadcast bool
  62. DanmuHost string
  63. }
  64. // ConfigSwitch config for query
  65. type ConfigSwitch struct {
  66. QueryExp int
  67. }
  68. // UserExpExpireConf config for cache expire
  69. type UserExpExpireConf struct {
  70. ExpireTime int32
  71. }
  72. // UserDhhExpireConf config for cache expire
  73. type UserDhhExpireConf struct {
  74. ExpireTime int32
  75. }
  76. func init() {
  77. flag.StringVar(&confPath, "conf", "", "default config path")
  78. flag.BoolVar(&TraceInit, "traceInit", true, "default trace init")
  79. }
  80. // Init init conf
  81. func Init() error {
  82. if confPath != "" {
  83. return local()
  84. }
  85. return remote()
  86. }
  87. func local() (err error) {
  88. _, err = toml.DecodeFile(confPath, &Conf)
  89. return
  90. }
  91. func remote() (err error) {
  92. if client, err = conf.New(); err != nil {
  93. return
  94. }
  95. if err = load(); err != nil {
  96. return
  97. }
  98. go func() {
  99. for range client.Event() {
  100. log.Info("config reload")
  101. if load() != nil {
  102. log.Error("config reload error (%v)", err)
  103. }
  104. }
  105. }()
  106. return
  107. }
  108. func load() (err error) {
  109. var (
  110. s string
  111. ok bool
  112. tmpConf *Config
  113. )
  114. if s, ok = client.Toml2(); !ok {
  115. return errors.New("load config center error")
  116. }
  117. if _, err = toml.Decode(s, &tmpConf); err != nil {
  118. return errors.New("could not decode config")
  119. }
  120. *Conf = *tmpConf
  121. return
  122. }