conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/http/blademaster/middleware/verify"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf config
  19. Conf = &Config{}
  20. )
  21. // Config .
  22. type Config struct {
  23. Log *log.Config
  24. BM *bm.ServerConfig
  25. Verify *verify.Config
  26. Redis *redis.Config
  27. Memcache *memcache.Config
  28. MySQL *sql.Config
  29. Ecode *ecode.Config
  30. CacheTTL *CacheTTL
  31. Biz *Biz
  32. }
  33. // CacheTTL .
  34. type CacheTTL struct {
  35. OrderTTL int32
  36. AssetTTL int32
  37. AssetRelationTTL int32
  38. AggrIncomeUserTTL int32
  39. AggrIncomeUserMonthlyTTL int32
  40. }
  41. // Biz .
  42. type Biz struct {
  43. RunCASTimes int64
  44. Pay struct {
  45. ID string
  46. Token string
  47. OrderTTL int32
  48. URLQuery string
  49. URLRefund string
  50. URLCancel string
  51. URLPayCallback string
  52. URLRefundCallback string
  53. }
  54. Price struct {
  55. PlatformTax map[string]float64
  56. }
  57. }
  58. func init() {
  59. flag.StringVar(&confPath, "conf", "", "default config path")
  60. }
  61. // Init init conf
  62. func Init() error {
  63. if confPath != "" {
  64. return local()
  65. }
  66. return remote()
  67. }
  68. func local() (err error) {
  69. _, err = toml.DecodeFile(confPath, &Conf)
  70. return
  71. }
  72. func remote() (err error) {
  73. if client, err = conf.New(); err != nil {
  74. return
  75. }
  76. if err = load(); err != nil {
  77. return
  78. }
  79. go func() {
  80. for range client.Event() {
  81. log.Info("config reload")
  82. if load() != nil {
  83. log.Error("config reload error (%v)", err)
  84. }
  85. }
  86. }()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Toml2(); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }