conf.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/job/main/member/model/block"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/queue/databus"
  14. "go-common/library/queue/databus/databusutil"
  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. // base
  27. // app
  28. App *bm.App
  29. Biz *BizConfig
  30. // Env
  31. Env string
  32. // goroutine sleep
  33. Tick time.Duration
  34. // log
  35. Xlog *log.Config
  36. // databus
  37. DataBus *databus.Config
  38. // databus
  39. AccDataBus *databus.Config
  40. // passport
  41. PassortDataBus *databus.Config
  42. // operate log.
  43. LogDatabus *databus.Config
  44. // operate log for publish
  45. PLogDatabus *databus.Config
  46. // add exp,pub by archive,history.
  47. ExpDatabus *databus.Config
  48. // login award ,pub by passport.
  49. LoginDatabus *databus.Config
  50. AwardDatabus *databus.Config
  51. RealnameDatabus *databus.Config
  52. // account notify to purge cache
  53. AccountNotify *databus.Config
  54. ShareMidDatabus *databus.Config
  55. // mc
  56. Memcache *Memcache
  57. // httpClinet
  58. HTTPClient *bm.ClientConfig
  59. Mysql *sql.Config
  60. AccCheckMysql *sql.Config
  61. AccMysql *sql.Config
  62. AsoMysql *sql.Config
  63. PasslogMysql *sql.Config
  64. // hbase
  65. // HBase *conf.HBase
  66. // redis
  67. Redis *redis.Config
  68. Databusutil *databusutil.Config
  69. On bool
  70. FeatureGates *FeatureGates
  71. SyncRange *SyncRange
  72. // bm
  73. BM *bm.ServerConfig
  74. // Report
  75. UserReport *databus.Config
  76. ManagerReport *databus.Config
  77. //block config.
  78. BlockMemcache *memcache.Config
  79. BlockDB *sql.Config
  80. BlockCreditDatabus *databus.Config
  81. BlockProperty *Property
  82. //realname
  83. RealnameRsaPriv []byte
  84. RealnameAlipayPub []byte
  85. RealnameAlipayBiliPriv []byte
  86. // Parsed Realname Infoc
  87. ParsedRealnameInfoc *infoc.Config
  88. }
  89. // Property .
  90. type Property struct {
  91. LimitExpireCheckLimit int
  92. LimitExpireCheckTick time.Duration
  93. CreditExpireCheckLimit int
  94. CreditExpireCheckTick time.Duration
  95. MSGURL string
  96. MSG *MSG
  97. Flag *struct {
  98. ExpireCheck bool
  99. CreditSub bool
  100. }
  101. }
  102. // MSG .
  103. type MSG struct {
  104. BlockRemove block.MSG
  105. }
  106. // Memcache memcache.
  107. type Memcache struct {
  108. *memcache.Config
  109. Expire time.Duration
  110. }
  111. // SyncRange syncRange.
  112. type SyncRange struct {
  113. Start int64
  114. End int64
  115. }
  116. // BizConfig biz common config
  117. type BizConfig struct {
  118. ExpiredBase int32
  119. ExpiredDetail int32
  120. IsFree bool
  121. AccprocCount int32
  122. ExpprocCount int32
  123. // realname alipay
  124. RealnameAlipayCheckTick time.Duration
  125. RealnameAlipayCheckLimit int
  126. RealnameAlipayAppID string
  127. RealnameAlipayGateway string
  128. }
  129. // FeatureGates is.
  130. type FeatureGates struct {
  131. DataFixer bool
  132. FaceCheck bool
  133. }
  134. func init() {
  135. flag.StringVar(&confPath, "conf", "", "default config path")
  136. }
  137. // Init create config instance.
  138. func Init() (err error) {
  139. if confPath != "" {
  140. return local()
  141. }
  142. return remote()
  143. }
  144. func local() (err error) {
  145. _, err = toml.DecodeFile(confPath, &Conf)
  146. return
  147. }
  148. func remote() (err error) {
  149. if client, err = conf.New(); err != nil {
  150. return
  151. }
  152. if err = load(); err != nil {
  153. return
  154. }
  155. // go func() {
  156. // for range client.Event() {
  157. // log.Info("config reload")
  158. // if load() != nil {
  159. // log.Error("config reload error (%v)", err)
  160. // }
  161. // }
  162. // }()
  163. return
  164. }
  165. func load() (err error) {
  166. var (
  167. s string
  168. ok bool
  169. tmpConf *Config
  170. )
  171. if s, ok = client.Toml2(); !ok {
  172. return errors.New("load config center error")
  173. }
  174. if _, err = toml.Decode(s, &tmpConf); err != nil {
  175. return errors.New("could not decode config")
  176. }
  177. tmpConf.RealnameRsaPriv, tmpConf.RealnameAlipayPub, tmpConf.RealnameAlipayBiliPriv = loadRealnameKey()
  178. *Conf = *tmpConf
  179. return
  180. }
  181. func loadRealnameKey() (rasPriv, alipayPub, alipayBiliPriv []byte) {
  182. var (
  183. emptyBytes = []byte("")
  184. )
  185. rasPriv, alipayPub, alipayBiliPriv = emptyBytes, emptyBytes, emptyBytes
  186. if client == nil {
  187. return
  188. }
  189. if str, ok := client.Value("realname.rsa.priv"); ok {
  190. rasPriv = []byte(str)
  191. }
  192. if str, ok := client.Value("realname.alipay.pub"); ok {
  193. alipayPub = []byte(str)
  194. }
  195. if str, ok := client.Value("realname.alipay.bili.priv"); ok {
  196. alipayBiliPriv = []byte(str)
  197. }
  198. return
  199. }