conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/tidb"
  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/rate"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus"
  18. "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. )
  21. // global var
  22. var (
  23. confPath string
  24. client *conf.Client
  25. // Conf config
  26. Conf = &Config{}
  27. )
  28. // Config config set
  29. type Config struct {
  30. // elk
  31. Log *log.Config
  32. // BM
  33. BM *bm.ServerConfig
  34. // rpc server
  35. RPCServer *rpc.ServerConfig
  36. GRPC *warden.ServerConfig
  37. // tracer
  38. Tracer *trace.Config
  39. // verify
  40. Verify *verify.Config
  41. Rate *rate.Config
  42. // redis
  43. Redis *Redis
  44. // memcache
  45. Memcache *Memcache
  46. // Tidb
  47. Tidb *tidb.Config
  48. // ecode
  49. Ecode *ecode.Config
  50. StatDatabus *databus.Config
  51. LikeDatabus *databus.Config
  52. ItemDatabus *databus.Config
  53. UserDatabus *databus.Config
  54. StatMerge *StatMerge
  55. // ThumbUp
  56. ThumbUp ThumbUp
  57. }
  58. // StatMerge .
  59. type StatMerge struct {
  60. Business string
  61. Target int64
  62. Sources []int64
  63. }
  64. // Memcache config
  65. type Memcache struct {
  66. *memcache.Config
  67. StatsExpire time.Duration
  68. }
  69. // Redis config
  70. type Redis struct {
  71. *redis.Config
  72. StatsExpire time.Duration
  73. UserLikesExpire time.Duration
  74. ItemLikesExpire time.Duration
  75. }
  76. // ThumbUp thumb up config
  77. type ThumbUp struct {
  78. }
  79. func init() {
  80. flag.StringVar(&confPath, "conf", "", "default config path")
  81. }
  82. // Init init conf
  83. func Init() error {
  84. if confPath != "" {
  85. return local()
  86. }
  87. return remote()
  88. }
  89. func local() (err error) {
  90. _, err = toml.DecodeFile(confPath, &Conf)
  91. return
  92. }
  93. func remote() (err error) {
  94. if client, err = conf.New(); err != nil {
  95. return
  96. }
  97. if err = load(); err != nil {
  98. return
  99. }
  100. go func() {
  101. for range client.Event() {
  102. log.Info("config reload")
  103. if load() != nil {
  104. log.Error("config reload error (%v)", err)
  105. }
  106. }
  107. }()
  108. return
  109. }
  110. func load() (err error) {
  111. var (
  112. s string
  113. ok bool
  114. tmpConf *Config
  115. )
  116. if s, ok = client.Toml2(); !ok {
  117. return errors.New("load config center error")
  118. }
  119. if _, err = toml.Decode(s, &tmpConf); err != nil {
  120. return errors.New("could not decode config")
  121. }
  122. *Conf = *tmpConf
  123. return
  124. }