conf.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/rpc/warden"
  9. "go-common/library/net/trace"
  10. "go-common/library/queue/databus"
  11. xtime "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // Conf global variable.
  15. var (
  16. Conf = &Config{}
  17. client *conf.Client
  18. confPath string
  19. )
  20. // Config struct of conf.
  21. type Config struct {
  22. Log *log.Config
  23. Tracer *trace.Config
  24. Databus *databus.Config
  25. SmsGRPC *warden.ClientConfig
  26. HTTPClient *bm.ClientConfig
  27. HTTPServer *bm.ServerConfig
  28. UserReport *databus.Config
  29. Wechat *wechat
  30. Provider *Provider
  31. Speedup *speedup
  32. Sms *sms
  33. }
  34. type sms struct {
  35. // PassportMobileURL 从passport获取用户手机号
  36. PassportMobileURL string
  37. // CallbackProc 处理回执的并发数
  38. CallbackProc int
  39. // SingleSendGoroutines 单发短信的并发数
  40. SingleSendProc int
  41. // BatchSendGoroutines 批量发送短信的并发数
  42. BatchSendProc int
  43. // MonitorProcDuration 定期监控databus有没有消费
  44. MonitorProcDuration xtime.Duration
  45. // Blacklist 黑名单手机号,用于压测
  46. Blacklist []string
  47. }
  48. type wechat struct {
  49. Token string
  50. Secret string
  51. Username string
  52. }
  53. // Provider provider conf
  54. type Provider struct {
  55. Providers []int32
  56. // meng wang
  57. MengWangSmsURL string
  58. MengWangSmsUser string
  59. MengWangSmsPwd string
  60. MengWangActURL string
  61. MengWangBatchURL string
  62. MengWangActUser string
  63. MengWangActPwd string
  64. MengWangInternationURL string
  65. MengWangInternationUser string
  66. MengWangInternationPwd string
  67. // chaung lan
  68. ChuangLanSmsURL string
  69. ChuangLanSmsUser string
  70. ChuangLanSmsPwd string
  71. ChuangLanActURL string
  72. ChuangLanActUser string
  73. ChuangLanActPwd string
  74. ChuangLanInternationURL string
  75. ChuangLanInternationUser string
  76. ChuangLanInternationPwd string
  77. // chuang lan callback
  78. ChuangLanSmsCallbackURL string
  79. ChuangLanActCallbackURL string
  80. ChuangLanInternationalCallbackURL string
  81. // meng wang callback
  82. MengWangSmsCallbackURL string
  83. MengWangActCallbackURL string
  84. MengWangInternationalCallbackURL string
  85. }
  86. // speedup network
  87. type speedup struct {
  88. Switch bool
  89. // meng wang
  90. MengWangSmsURL string
  91. MengWangActURL string
  92. MengWangBatchURL string
  93. MengWangInternationURL string
  94. // chaung lan
  95. ChuangLanSmsURL string
  96. ChuangLanInternationURL string
  97. ChuangLanActURL string
  98. // meng wang callback
  99. MengWangSmsCallbackURL string
  100. MengWangActCallbackURL string
  101. MengWangInternationalCallbackURL string
  102. // chaung lan callback
  103. ChuangLanSmsCallbackURL string
  104. ChuangLanActCallbackURL string
  105. ChuangLanInternationalCallbackURL string
  106. }
  107. func init() {
  108. flag.StringVar(&confPath, "conf", "", "default config path")
  109. }
  110. // Init create config instance.
  111. func Init() (err error) {
  112. if confPath != "" {
  113. return local()
  114. }
  115. return remote()
  116. }
  117. func local() (err error) {
  118. _, err = toml.DecodeFile(confPath, &Conf)
  119. return
  120. }
  121. func remote() (err error) {
  122. if client, err = conf.New(); err != nil {
  123. return
  124. }
  125. err = load()
  126. return
  127. }
  128. func load() (err error) {
  129. var (
  130. s string
  131. ok bool
  132. tmpConf *Config
  133. )
  134. if s, ok = client.Toml2(); !ok {
  135. return errors.New("load config center error")
  136. }
  137. if _, err = toml.Decode(s, &tmpConf); err != nil {
  138. return errors.New("could not decode config")
  139. }
  140. *Conf = *tmpConf
  141. return
  142. }