conf.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // Conf global variable.
  19. var (
  20. Conf = &Config{}
  21. client *conf.Client
  22. confPath string
  23. )
  24. // Config struct of conf.
  25. type Config struct {
  26. AccountIntranetURI string
  27. AccountURI string
  28. VipURI string
  29. PayURL string
  30. NotifyURL string
  31. PayInfo *PayInfo
  32. // base
  33. // host
  34. Host *Host
  35. // log
  36. Xlog *log.Config
  37. // tracer
  38. Tracer *trace.Config
  39. // Verify
  40. Verify *verify.Config
  41. // http
  42. BM *bm.ServerConfig
  43. // RPCServer rpc server
  44. RPCServer *rpc.ServerConfig
  45. // MySQL mysql
  46. MySQL *sql.Config
  47. // Redis .
  48. Redis *Redis
  49. // Memcache memcache.
  50. Memcache *Memcache
  51. // HTTPClient http client
  52. HTTPClient *bm.ClientConfig
  53. // GORPCClient
  54. GORPCClient *GORPCClient
  55. // MedalCache
  56. MedalCache *LocalCache
  57. // EquipCache
  58. EquipCache *LocalCache
  59. // AccountNotify account notify.
  60. AccountNotify *databus.Config
  61. }
  62. // GORPCClient .
  63. type GORPCClient struct {
  64. Member *rpc.ClientConfig
  65. Coin *rpc.ClientConfig
  66. Point *rpc.ClientConfig
  67. }
  68. // Host define host conf.
  69. type Host struct {
  70. MessageCo string
  71. AccountCoURI string
  72. APICoURI string
  73. LiveAPICo string
  74. }
  75. // PayInfo pay basic info
  76. type PayInfo struct {
  77. MerchantID string
  78. MerchantProductID string
  79. CallBackURL string
  80. }
  81. // Redis .
  82. type Redis struct {
  83. *redis.Config
  84. InviteExpire time.Duration
  85. PendantExpire time.Duration
  86. }
  87. // LocalCache .
  88. type LocalCache struct {
  89. Size int
  90. Expire time.Duration
  91. }
  92. // Memcache define memcache conf.
  93. type Memcache struct {
  94. *memcache.Config
  95. MedalExpire time.Duration
  96. PointExpire time.Duration
  97. }
  98. func local() (err error) {
  99. _, err = toml.DecodeFile(confPath, &Conf)
  100. return
  101. }
  102. func remote() (err error) {
  103. if client, err = conf.New(); err != nil {
  104. return
  105. }
  106. if err = load(); err != nil {
  107. return
  108. }
  109. go func() {
  110. for range client.Event() {
  111. log.Info("config reload")
  112. if load() != nil {
  113. log.Error("config reload error (%v)", err)
  114. }
  115. }
  116. }()
  117. return
  118. }
  119. func load() (err error) {
  120. var (
  121. s string
  122. ok bool
  123. tmpConf *Config
  124. )
  125. if s, ok = client.Toml2(); !ok {
  126. return errors.New("load config center error")
  127. }
  128. if _, err = toml.Decode(s, &tmpConf); err != nil {
  129. return errors.New("could not decode config")
  130. }
  131. *Conf = *tmpConf
  132. return
  133. }
  134. func init() {
  135. flag.StringVar(&confPath, "conf", "", "default config path")
  136. }
  137. // Init int config
  138. func Init() error {
  139. if confPath != "" {
  140. return local()
  141. }
  142. return remote()
  143. }