conf.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/antispam"
  14. "go-common/library/net/http/blademaster/middleware/auth"
  15. "go-common/library/net/netutil"
  16. "go-common/library/net/rpc"
  17. "go-common/library/net/rpc/warden"
  18. "go-common/library/net/trace"
  19. "go-common/library/queue/databus"
  20. "go-common/library/time"
  21. "github.com/BurntSushi/toml"
  22. )
  23. // Conf global variable.
  24. var (
  25. Conf = &Config{}
  26. client *conf.Client
  27. confPath string
  28. )
  29. // Config struct of conf.
  30. type Config struct {
  31. App *bm.App
  32. Host *Host
  33. Log *log.Config
  34. Tracer *trace.Config
  35. Infoc2 *infoc.Config
  36. Ecode *ecode.Config
  37. BM *bm.ServerConfig
  38. HTTPClient *HTTPClient
  39. RPCClient *RPC
  40. DataBus *DataSource
  41. Mysql *sql.Config
  42. Memcache *Memcache
  43. Redis *Redis
  44. AuthN *auth.Config
  45. Antispam *antispam.Config
  46. Geetest *Geetest
  47. Answer *Answer
  48. Question *Question
  49. Backoff *netutil.BackoffConfig
  50. Report *databus.Config
  51. AccountRPC *warden.ClientConfig
  52. Captcha *bm.ClientConfig
  53. }
  54. // RPC config
  55. type RPC struct {
  56. Member *rpc.ClientConfig
  57. Account *rpc.ClientConfig
  58. }
  59. // Answer conf.
  60. type Answer struct {
  61. Captcha bool // true:only use bili captcha
  62. Debug bool
  63. Duration int64
  64. BlockedTimestamp int64
  65. BaseNum int
  66. BaseExtraPassNum int
  67. BaseExtraNoPassNum int
  68. ProNum int
  69. BaseExtraScore int
  70. BaseExtraPassCount int
  71. ExtraNum int
  72. MaxRetries int
  73. CaptchaTokenURL string
  74. CaptchaVerifyURL string
  75. }
  76. // Redis conf.
  77. type Redis struct {
  78. *redis.Config
  79. Expire time.Duration
  80. AnsCountExpire time.Duration
  81. AnsAddFlagCountExpire time.Duration
  82. }
  83. // Memcache conf.
  84. type Memcache struct {
  85. *memcache.Config
  86. Expire time.Duration
  87. AnswerBolckExpire time.Duration
  88. }
  89. // Question conf.
  90. type Question struct {
  91. // question total count tick
  92. TcQestTick time.Duration
  93. RankQestTick time.Duration
  94. }
  95. // HTTPClient conf.
  96. type HTTPClient struct {
  97. Normal *bm.ClientConfig
  98. Slow *bm.ClientConfig
  99. }
  100. // Host conf.
  101. type Host struct {
  102. Geetest string
  103. Account string
  104. ExtraIds string
  105. API string
  106. }
  107. // Geetest geetest id & key
  108. type Geetest struct {
  109. PC GeetestConfig
  110. H5 GeetestConfig
  111. }
  112. // GeetestConfig conf.
  113. type GeetestConfig struct {
  114. CaptchaID string
  115. PrivateKEY string
  116. }
  117. // App bilibili intranet authorization.
  118. type App struct {
  119. Key string
  120. Secret string
  121. }
  122. // DataSource .
  123. type DataSource struct {
  124. ExtraAnswer *databus.Config
  125. }
  126. func local() (err error) {
  127. _, err = toml.DecodeFile(confPath, &Conf)
  128. return
  129. }
  130. func remote() (err error) {
  131. if client, err = conf.New(); err != nil {
  132. return
  133. }
  134. if err = load(); err != nil {
  135. return
  136. }
  137. return
  138. }
  139. func load() (err error) {
  140. var (
  141. s string
  142. ok bool
  143. tmpConf *Config
  144. )
  145. if s, ok = client.Toml2(); !ok {
  146. return errors.New("load config center error")
  147. }
  148. if _, err = toml.Decode(s, &tmpConf); err != nil {
  149. return errors.New("could not decode config")
  150. }
  151. *Conf = *tmpConf
  152. return
  153. }
  154. func init() {
  155. flag.StringVar(&confPath, "conf", "", "default config path")
  156. }
  157. // Init int config
  158. func Init() error {
  159. if confPath != "" {
  160. return local()
  161. }
  162. return remote()
  163. }