conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. xtime "go-common/library/time"
  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. Verify *verify.Config
  27. Tracer *trace.Config
  28. Memcache *memcache.Config
  29. MySQL *sql.Config
  30. GRPC *warden.ServerConfig
  31. HTTPClient *bm.ClientConfig
  32. MVIPClient *warden.ClientConfig
  33. ACCClient *warden.ClientConfig
  34. YST *YstConfig
  35. PAY *PayConfig
  36. Ticker *TickerConfig
  37. MVIP *MVIPConfig
  38. CacheTTL *CacheTTL
  39. }
  40. // CacheTTL contains ttl configs.
  41. type CacheTTL struct {
  42. UserInfoTTL int32
  43. PayParamTTL int32
  44. LockTTL int32
  45. }
  46. // TickerConfig contains durations configs of ticker proc.
  47. type TickerConfig struct {
  48. PanelRefreshDuration string
  49. UnpaidDurationStime string
  50. UnpaidDurationEtime string
  51. UnpaidRefreshDuratuion string
  52. }
  53. // MVIPConfig contains mvip configs.
  54. type MVIPConfig struct {
  55. BatchIdsMap map[string]int
  56. BatchUserInfoUrl string
  57. }
  58. // YstConfig contains yst configs.
  59. type YstConfig struct {
  60. Domain string
  61. Key string
  62. }
  63. // PayConfig contains pay configs.
  64. type PayConfig struct {
  65. PayExpireDuration string
  66. RenewFromDuration string
  67. RenewToDuration string
  68. OrderRateFromDuration xtime.Duration
  69. OrderRateMaxNumber int
  70. QrURL string
  71. GuestQrURL string
  72. }
  73. func init() {
  74. flag.StringVar(&confPath, "conf", "", "default config path")
  75. }
  76. // Init init conf
  77. func Init() error {
  78. if confPath != "" {
  79. return local()
  80. }
  81. return remote()
  82. }
  83. func local() (err error) {
  84. _, err = toml.DecodeFile(confPath, &Conf)
  85. return
  86. }
  87. func remote() (err error) {
  88. if client, err = conf.New(); err != nil {
  89. return
  90. }
  91. if err = load(); err != nil {
  92. return
  93. }
  94. go func() {
  95. for range client.Event() {
  96. log.Info("config reload")
  97. if load() != nil {
  98. log.Error("config reload error (%v)", err)
  99. }
  100. }
  101. }()
  102. return
  103. }
  104. func load() (err error) {
  105. var (
  106. s string
  107. ok bool
  108. tmpConf *Config
  109. )
  110. if s, ok = client.Toml2(); !ok {
  111. return errors.New("load config center error")
  112. }
  113. if _, err = toml.Decode(s, &tmpConf); err != nil {
  114. return errors.New("could not decode config")
  115. }
  116. *Conf = *tmpConf
  117. return
  118. }