conf.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/elastic"
  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/permit"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus"
  18. xtime "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. )
  21. var (
  22. confPath string
  23. client *conf.Client
  24. // Conf config
  25. Conf = &Config{}
  26. )
  27. // Config .
  28. type Config struct {
  29. Log *log.Config
  30. BM *bm.ServerConfig
  31. Verify *verify.Config
  32. Tracer *trace.Config
  33. Redis *redis.Config
  34. MySQL *sql.Config
  35. Ecode *ecode.Config
  36. // HTTPClientConfig
  37. HTTPClientConfig *bm.ClientConfig
  38. // AccRecover request URL info
  39. AccRecover *AccRecover
  40. // MailConfig
  41. MailConf *Mail
  42. // CaptchaConf
  43. CaptchaConf *Captcha
  44. // RPC config
  45. LocationRPC *rpc.ClientConfig
  46. // grpc
  47. MemberGRPC *warden.ClientConfig
  48. AccountGRPC *warden.ClientConfig
  49. // ChanSize
  50. ChanSize *ChanSize
  51. // Auth
  52. Auth *permit.Config
  53. AESEncode *AESEncode
  54. // elastic config
  55. Elastic *elastic.Config
  56. // Bfs
  57. Bfs *Bfs
  58. // DataBus databus
  59. DataBus *DataBus
  60. }
  61. // ChanSize mail send channel size.
  62. type ChanSize struct {
  63. MailMsg int64
  64. }
  65. // AccRecover is a url config to request java api
  66. type AccRecover struct {
  67. MidInfoURL string
  68. UpPwdURL string
  69. UpBatchPwdURL string
  70. CheckSafeURL string
  71. GameURL string
  72. CheckRegURL string
  73. CheckUserURL string
  74. CheckCardStatusURL string
  75. CheckCardURL string
  76. CheckPwdURL string
  77. GetLoginIPURL string
  78. GetUserInfoURL string
  79. }
  80. // Mail 邮件配置
  81. type Mail struct {
  82. Host string
  83. Port int
  84. Username, Password string
  85. }
  86. // Captcha 验证码配置
  87. type Captcha struct {
  88. TokenBID string
  89. TokenURL string
  90. VerifyURL string
  91. }
  92. // AESEncode aes encode
  93. type AESEncode struct {
  94. AesKey string
  95. Salt string
  96. }
  97. // Bfs Bfs.
  98. type Bfs struct {
  99. Timeout xtime.Duration
  100. Bucket string
  101. Addr string
  102. Key string
  103. Secret string
  104. }
  105. // DataBus is
  106. type DataBus struct {
  107. UserActLog *databus.Config
  108. }
  109. func init() {
  110. flag.StringVar(&confPath, "conf", "", "default config path")
  111. }
  112. // Init init conf
  113. func Init() error {
  114. if confPath != "" {
  115. return local()
  116. }
  117. return remote()
  118. }
  119. func local() (err error) {
  120. _, err = toml.DecodeFile(confPath, &Conf)
  121. return
  122. }
  123. func remote() (err error) {
  124. if client, err = conf.New(); err != nil {
  125. return
  126. }
  127. if err = load(); err != nil {
  128. return
  129. }
  130. go func() {
  131. for range client.Event() {
  132. log.Info("config reload")
  133. if load() != nil {
  134. log.Error("config reload error (%v)", err)
  135. }
  136. }
  137. }()
  138. return
  139. }
  140. func load() (err error) {
  141. var (
  142. s string
  143. ok bool
  144. tmpConf *Config
  145. )
  146. if s, ok = client.Toml2(); !ok {
  147. return errors.New("load config center error")
  148. }
  149. if _, err = toml.Decode(s, &tmpConf); err != nil {
  150. return errors.New("could not decode config")
  151. }
  152. *Conf = *tmpConf
  153. return
  154. }