conf.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/elastic"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. "go-common/library/log/infoc"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/trace"
  16. "go-common/library/queue/databus"
  17. xtime "go-common/library/time"
  18. "github.com/BurntSushi/toml"
  19. )
  20. var (
  21. confPath string
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. type Config struct {
  26. // Env
  27. Env string
  28. // db
  29. MySQL *MySQL
  30. // show XLog
  31. Log *log.Config
  32. // tracer
  33. Tracer *trace.Config
  34. // tick time
  35. Tick xtime.Duration
  36. // httpClinet
  37. HTTPClient *bm.ClientConfig
  38. // HTTPTelecom
  39. HTTPTelecom *bm.ClientConfig
  40. // HTTPBroadband
  41. HTTPBroadband *bm.ClientConfig
  42. // HTTPUnicom
  43. HTTPUnicom *bm.ClientConfig
  44. // HTTPUnicom
  45. HTTPActive *bm.ClientConfig
  46. // bm http
  47. BM *HTTPServers
  48. // rpc account
  49. AccountRPC *rpc.ClientConfig
  50. // seq
  51. SeqRPC *rpc.ClientConfig
  52. // host
  53. Host *Host
  54. // ecode
  55. Ecode *ecode.Config
  56. // Report
  57. Report *databus.Config
  58. // iplimit
  59. IPLimit *IPLimit
  60. // infoc2
  61. UnicomUserInfoc2 *infoc.Config
  62. UnicomIpInfoc2 *infoc.Config
  63. UnicomPackInfoc *infoc.Config
  64. // Seq
  65. Seq *Seq
  66. // Telecom
  67. Telecom *Telecom
  68. // Redis
  69. Redis *Redis
  70. // mc
  71. Memcache *Memcache
  72. // reddot
  73. Reddot *Reddot
  74. // unicom
  75. Unicom *Unicom
  76. ES *elastic.Config
  77. // databus
  78. UnicomDatabus *databus.Config
  79. }
  80. type Host struct {
  81. APICo string
  82. Dotin string
  83. Live string
  84. APILive string
  85. Telecom string
  86. Unicom string
  87. UnicomFlow string
  88. Broadband string
  89. Sms string
  90. Mall string
  91. TelecomReturnURL string
  92. TelecomCancelPayURL string
  93. }
  94. type HTTPServers struct {
  95. Outer *bm.ServerConfig
  96. }
  97. type Seq struct {
  98. BusinessID int64
  99. Token string
  100. }
  101. // App bilibili intranet authorization.
  102. type App struct {
  103. Key string
  104. Secret string
  105. }
  106. type MySQL struct {
  107. Show *sql.Config
  108. }
  109. type IPLimit struct {
  110. MobileIPFile string
  111. Addrs map[string][]string
  112. }
  113. type Reddot struct {
  114. StartTime string
  115. EndTime string
  116. }
  117. type Unicom struct {
  118. KeyExpired xtime.Duration
  119. FlowWait xtime.Duration
  120. }
  121. type Telecom struct {
  122. KeyExpired xtime.Duration
  123. PayKeyExpired xtime.Duration
  124. SMSTemplate string
  125. SMSMsgTemplate string
  126. SMSFlowTemplate string
  127. SMSOrderTemplateOK string
  128. FlowPercentage int
  129. Area map[string][]string
  130. }
  131. type Redis struct {
  132. Recommend *struct {
  133. *redis.Config
  134. Expire xtime.Duration
  135. }
  136. }
  137. type Memcache struct {
  138. Operator *struct {
  139. *memcache.Config
  140. Expire xtime.Duration
  141. }
  142. }
  143. func init() {
  144. flag.StringVar(&confPath, "conf", "", "default config path")
  145. }
  146. // Init init config.
  147. func Init() (err error) {
  148. if confPath != "" {
  149. _, err = toml.DecodeFile(confPath, &Conf)
  150. return
  151. }
  152. err = remote()
  153. return
  154. }
  155. func remote() (err error) {
  156. if client, err = conf.New(); err != nil {
  157. return
  158. }
  159. if err = load(); err != nil {
  160. return
  161. }
  162. go func() {
  163. for range client.Event() {
  164. log.Info("config reload")
  165. if load() != nil {
  166. log.Error("config reload error (%v)", err)
  167. }
  168. }
  169. }()
  170. return
  171. }
  172. func load() (err error) {
  173. var (
  174. s string
  175. ok bool
  176. tmpConf *Config
  177. )
  178. if s, ok = client.Toml2(); !ok {
  179. return errors.New("load config center error")
  180. }
  181. if _, err = toml.Decode(s, &tmpConf); err != nil {
  182. return errors.New("could not decode config")
  183. }
  184. *Conf = *tmpConf
  185. return
  186. }