conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/rpc/warden"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // global var
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config config set
  24. type Config struct {
  25. // elk
  26. Log *log.Config
  27. // http
  28. BM *bm.ServerConfig
  29. // memcache
  30. Memcache *Memcache
  31. // MySQL
  32. MySQL *sql.Config
  33. // ecode
  34. Ecode *ecode.Config
  35. // biz property.
  36. Property *Property
  37. // rpc server
  38. RPCServer *rpc.ServerConfig
  39. // http client
  40. HTTPClient *bm.ClientConfig
  41. // vipinfo grpc
  42. VipinfoRPC *warden.ClientConfig
  43. NewYearConf *NewYearConf
  44. // grpc server
  45. WardenServer *warden.ServerConfig
  46. Platform map[string]string
  47. }
  48. // NewYearConf .
  49. type NewYearConf struct {
  50. ActID int64
  51. StartTime int64
  52. EndTime int64
  53. RandNum int64
  54. NoVipBatchToken1 string
  55. NoVipBatchToken3 string
  56. NoVipBatchToken12 string
  57. More180BatchToken1 string
  58. More180BatchToken3 string
  59. More180BatchToken12 string
  60. Less180BatchToken1 string
  61. Less180BatchToken3 string
  62. Less180BatchToken12 string
  63. MonthBatchToken1 string
  64. MonthBatchToken3 string
  65. MonthBatchToken12 string
  66. }
  67. // Property def.
  68. type Property struct {
  69. MessageURL string
  70. CaptchaTokenURL string
  71. CaptchaVerifyURL string
  72. CaptchaBID string
  73. }
  74. // Memcache memcache
  75. type Memcache struct {
  76. *memcache.Config
  77. Expire xtime.Duration
  78. PrizeExpire xtime.Duration
  79. }
  80. func init() {
  81. flag.StringVar(&confPath, "conf", "", "default config path")
  82. }
  83. // Init init conf
  84. func Init() error {
  85. if confPath != "" {
  86. return local()
  87. }
  88. return remote()
  89. }
  90. func local() (err error) {
  91. _, err = toml.DecodeFile(confPath, &Conf)
  92. return
  93. }
  94. func remote() (err error) {
  95. if client, err = conf.New(); err != nil {
  96. return
  97. }
  98. if err = load(); err != nil {
  99. return
  100. }
  101. return
  102. }
  103. func load() (err error) {
  104. var (
  105. s string
  106. ok bool
  107. tmpConf *Config
  108. )
  109. if s, ok = client.Toml2(); !ok {
  110. return errors.New("load config center error")
  111. }
  112. if _, err = toml.Decode(s, &tmpConf); err != nil {
  113. return errors.New("could not decode config")
  114. }
  115. *Conf = *tmpConf
  116. return
  117. }