conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/net/rpc/liverpc"
  6. "go-common/library/database/sql"
  7. "go-common/library/queue/databus"
  8. "go-common/library/cache/redis"
  9. "go-common/library/conf"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/trace"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config .
  24. type Config struct {
  25. Log *log.Config
  26. BM *bm.ServerConfig
  27. Verify *verify.Config
  28. Tracer *trace.Config
  29. Redis *Redis
  30. Database *Database
  31. Ecode *ecode.Config
  32. Cfg *Cfg
  33. // databus
  34. GiftPaySub *databus.Config
  35. GiftFreeSub *databus.Config
  36. AddCapsuleSub *databus.Config
  37. UserReport *databus.Config
  38. LiveRpc map[string]*liverpc.ClientConfig
  39. HTTPClient *bm.ClientConfig
  40. CouponConf *CouponConfig
  41. }
  42. // CouponConfig .
  43. type CouponConfig struct {
  44. Url string
  45. Coupon map[string]string
  46. }
  47. // Database mysql
  48. type Database struct {
  49. Lottery *sql.Config
  50. }
  51. // Redis redis
  52. type Redis struct {
  53. Lottery *redis.Config
  54. }
  55. // Cfg def
  56. type Cfg struct {
  57. // ExpireCountFrequency crontab frequency
  58. ExpireCountFrequency string
  59. CouponRetryFrequency string
  60. ConsumerProcNum int64
  61. }
  62. func init() {
  63. flag.StringVar(&confPath, "conf", "", "default config path")
  64. }
  65. // Init init conf
  66. func Init() error {
  67. if confPath != "" {
  68. return local()
  69. }
  70. return remote()
  71. }
  72. func local() (err error) {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. func remote() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. return
  79. }
  80. if err = load(); err != nil {
  81. return
  82. }
  83. go func() {
  84. for range client.Event() {
  85. log.Info("config reload")
  86. if load() != nil {
  87. log.Error("config reload error (%v)", err)
  88. }
  89. }
  90. }()
  91. return
  92. }
  93. func load() (err error) {
  94. var (
  95. s string
  96. ok bool
  97. tmpConf *Config
  98. )
  99. if s, ok = client.Toml2(); !ok {
  100. return errors.New("load config center error")
  101. }
  102. if _, err = toml.Decode(s, &tmpConf); err != nil {
  103. return errors.New("could not decode config")
  104. }
  105. *Conf = *tmpConf
  106. return
  107. }