conf.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/queue/databus"
  14. "go-common/library/queue/databus/databusutil"
  15. xtime "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. VipURI string
  27. // base
  28. App *bm.App
  29. // log
  30. Xlog *log.Config
  31. // http
  32. BM *bm.ServerConfig
  33. // db
  34. NewMysql *sql.Config
  35. //old db
  36. OldMysql *sql.Config
  37. // http client
  38. HTTPClient *bm.ClientConfig
  39. //Property
  40. Property *Property
  41. URLConf *URLConf
  42. //databus group config
  43. DatabusUtil *databusutil.Config
  44. //point databus
  45. Databus *DataSource
  46. // mc
  47. Memcache *Memcache
  48. // redis
  49. Redis *Redis
  50. PayConf *PayConf
  51. // rpc clients
  52. RPCClient2 *RPC
  53. // grpc
  54. VipClient *warden.ClientConfig
  55. }
  56. //RPC rpc clients.
  57. type RPC struct {
  58. Coupon *rpc.ClientConfig
  59. }
  60. //URLConf url conf
  61. type URLConf struct {
  62. PayCoURL string
  63. PayURL string
  64. MsgURL string
  65. MallURL string
  66. AccountURL string
  67. APICoURL string
  68. OldVipCoURL string
  69. }
  70. // Property config for biz logic.
  71. type Property struct {
  72. UpdateUserInfoCron string
  73. AutoRenewCron string
  74. SendMessageCron string
  75. SendBcoinCron string
  76. WillExpireMsgCron string
  77. HadExpiredMsgCron string
  78. PushDataCron string
  79. EleEompensateCron string
  80. HandlerThread int
  81. ReadThread int
  82. Retry int
  83. FrozenExpire xtime.Duration
  84. FrozenDate xtime.Duration
  85. FrozenLimit int64
  86. FrozenCron string
  87. PayMapping map[string]string
  88. MsgURL string
  89. ActivityID int64
  90. AnnualVipBcoinDay int
  91. AnnualVipBcoinCouponMoney int
  92. PayCoURL string
  93. SalaryDay int
  94. AnnualVipSalaryCount int
  95. NormalVipSalaryCount int
  96. SalaryVideoCouponnIterval xtime.Duration
  97. SalaryVideoCouponCron string
  98. MsgOpen bool
  99. BatchSize int
  100. SalaryCouponMaps map[string]map[string]int64 // map[coupontype]map[viptype]salarycount
  101. SalaryCouponTypes []int8
  102. SalaryCouponBatchNoMaps map[string]string // map[coupontype]batchnofmt
  103. SalaryCouponMsgTitleMaps map[string]string // map[ coupontype]msgTitle
  104. SalaryCouponMsgContentMaps map[string]string // map[coupontype]msgsContent
  105. SalaryCouponMsgSupplyContentMaps map[string]string // map[coupontype]msgsContent
  106. SalaryCouponURL string
  107. ActiveStartTime string
  108. SendMedalEndTime string
  109. SendVipbuyEndTime string
  110. SummerActiveStartTime string
  111. SummerActiveEndTime string
  112. SendCodeStartTime string
  113. SendCodeEndTime string
  114. CouponIDs []string
  115. MedalID int64
  116. CodeExchangeMap map[string][]int64
  117. CodeExchangeTimeMap map[string]int
  118. CodeExchangePicMap map[string]string
  119. VipbuyExchangeNameMap map[string]string
  120. GrayScope int64
  121. PushToken string
  122. BusinessID int64
  123. SplitPush int
  124. UpdateDB bool
  125. NotGrantLimit int
  126. }
  127. //PayConf pay conf info
  128. type PayConf struct {
  129. BasicURL string
  130. CustomerID string
  131. Token string
  132. NotifyURL string
  133. OrderNotifyURL string
  134. SignNotifyURL string
  135. PlanID int32
  136. ProductID string
  137. Version string
  138. }
  139. // Memcache memcache
  140. type Memcache struct {
  141. *memcache.Config
  142. Expire xtime.Duration
  143. }
  144. // Redis redis
  145. type Redis struct {
  146. *redis.Config
  147. Expire xtime.Duration
  148. }
  149. // DataSource databus config zone.
  150. type DataSource struct {
  151. AccLogin *databus.Config
  152. OldVipBinLog *databus.Config
  153. SalaryCoupon *databus.Config
  154. NewVipBinLog *databus.Config
  155. AccountNotify *databus.Config
  156. CouponNotify *databus.Config
  157. AutoRenew *databus.Config
  158. }
  159. func init() {
  160. flag.StringVar(&confPath, "conf", "", "default config path")
  161. }
  162. // Init create config instance.
  163. func Init() (err error) {
  164. if confPath != "" {
  165. return local()
  166. }
  167. return remote()
  168. }
  169. func local() (err error) {
  170. _, err = toml.DecodeFile(confPath, &Conf)
  171. return
  172. }
  173. func remote() (err error) {
  174. if client, err = conf.New(); err != nil {
  175. return
  176. }
  177. if err = load(); err != nil {
  178. return
  179. }
  180. go func() {
  181. for range client.Event() {
  182. log.Info("config reload")
  183. if load() != nil {
  184. log.Error("config reload error (%v)", err)
  185. }
  186. }
  187. }()
  188. return
  189. }
  190. func load() (err error) {
  191. var (
  192. s string
  193. ok bool
  194. tmpConf *Config
  195. )
  196. if s, ok = client.Toml2(); !ok {
  197. return errors.New("load config center error")
  198. }
  199. if _, err = toml.Decode(s, &tmpConf); err != nil {
  200. return errors.New("could not decode config")
  201. }
  202. *Conf = *tmpConf
  203. return
  204. }