conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/warden"
  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. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Redis *redis.Config
  27. Memcache *memcache.Config
  28. MemcacheRank *memcache.Config
  29. MySQL *sql.Config
  30. MySQLRank *sql.Config
  31. MySQLRankOld *sql.Config
  32. GRPCUGCPayRank *warden.ClientConfig
  33. Ecode *ecode.Config
  34. BinlogMQ *databus.Config
  35. ElecBinlogMQ *databus.Config
  36. CacheTTL *CacheTTL
  37. Biz *Biz
  38. }
  39. // Biz .
  40. type Biz struct {
  41. RunCASTimes int64
  42. AccountUserMin int64
  43. Cron *struct {
  44. TaskDailyBill string
  45. TaskAccountUser string
  46. TaskAccountBiz string
  47. TaskMonthlyBill string
  48. TaskRechargeShell string
  49. }
  50. Tax *struct {
  51. AssetRate float64
  52. }
  53. Pay struct {
  54. ID string
  55. Token string
  56. CheckOrderURL string
  57. CheckRefundOrderURL string
  58. RechargeShellURL string
  59. RechargeCallbackURL string
  60. OrderQueryURL string
  61. }
  62. Task *struct {
  63. DailyBillPrefix string
  64. DailyBillOffset int
  65. AccountUserPrefix string
  66. AccountBizPrefix string
  67. MonthBillPrefix string
  68. MonthBillOffset int
  69. RechargeShellPrefix string
  70. RechargeShellOffset int
  71. }
  72. }
  73. // CacheTTL .
  74. type CacheTTL struct {
  75. ElecOrderIDTTL int32
  76. }
  77. func init() {
  78. flag.StringVar(&confPath, "conf", "", "default config path")
  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. }