conf.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/antispam"
  13. "go-common/library/net/http/blademaster/middleware/auth"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/trace"
  18. "go-common/library/queue/databus"
  19. "go-common/library/time"
  20. "github.com/BurntSushi/toml"
  21. )
  22. // Conf global variable.
  23. var (
  24. Conf = &Config{}
  25. client *conf.Client
  26. confPath string
  27. )
  28. // Config struct of conf.
  29. type Config struct {
  30. // base
  31. // host
  32. Host *Host
  33. // log
  34. Xlog *log.Config
  35. // AuthN
  36. AuthN *auth.Config
  37. // Verify
  38. Verify *verify.Config
  39. // http
  40. BM *HTTPServers
  41. // db
  42. Mysql *sql.Config
  43. // mc
  44. Memcache *Memcache
  45. //redis
  46. Redis *Redis
  47. // databus
  48. DataBus *databus.Config
  49. // tracer
  50. Tracer *trace.Config
  51. // bm client
  52. HTTPClient *bm.ClientConfig
  53. // Judge conf
  54. Judge *Judge
  55. // ecodes
  56. Ecode *ecode.Config
  57. // Antispam
  58. Antispam *antispam.Config
  59. // TagID
  60. TagID *TagID
  61. // Property
  62. Property *Property
  63. // rpc client
  64. RPCClient2 *RPC
  65. // GRPCClient
  66. GRPCClient *GRPC
  67. }
  68. // Redis define redis conf.
  69. type Redis struct {
  70. *redis.Config
  71. Expire time.Duration
  72. }
  73. // Memcache define memcache conf.
  74. type Memcache struct {
  75. *memcache.Config
  76. UserExpire time.Duration
  77. MinCommonExpire time.Duration
  78. CommonExpire time.Duration
  79. }
  80. // Host define host conf.
  81. type Host struct {
  82. MessageURI string
  83. BigDataURI string
  84. APICoURI string
  85. ManagersURI string
  86. }
  87. // Judge define judge conf.
  88. type Judge struct {
  89. ConfTimer time.Duration // 定时load数据时间间隔
  90. ReservedTime time.Duration // 结案前N分钟停止获取case
  91. LoadManagerTime time.Duration // load manager user的时间间隔
  92. CaseGiveHours int64 // 案件发放时长
  93. CaseCheckTime int64 // 单案审核时长
  94. JuryRatio int64 // 投准率下限
  95. JudgeRadio int64 // 判决阙值
  96. CaseVoteMin int64 // 案件投票数下限
  97. CaseObtainMax int64 // 每日获取案件数
  98. CaseVoteMax int64 // 结案投票数
  99. JuryApplyMax int64 // 每日发放风纪委上限
  100. CaseLoadMax int // 案件发放最大队列数
  101. CaseLoadSwitch int8 // 案件发放进入队列开关
  102. VoteNum
  103. }
  104. // VoteNum .
  105. type VoteNum struct {
  106. RateS int8 `json:"rate_s"`
  107. RateA int8 `json:"rate_a"`
  108. RateB int8 `json:"rate_b"`
  109. RateC int8 `json:"rate_c"`
  110. RateD int8 `json:"rate_d"`
  111. }
  112. // RPC rpc client config.
  113. type RPC struct {
  114. Archive *rpc.ClientConfig
  115. Member *rpc.ClientConfig
  116. }
  117. // GRPC .
  118. type GRPC struct {
  119. Filter *warden.ClientConfig
  120. Account *warden.ClientConfig
  121. }
  122. // HTTPServers Http Servers
  123. type HTTPServers struct {
  124. Inner *bm.ServerConfig
  125. Local *bm.ServerConfig
  126. }
  127. // TagID is workflow id .
  128. type TagID struct {
  129. Reply int64
  130. DM int64
  131. Msg int64
  132. Tag int64
  133. Member int64
  134. Archive int64
  135. Music int64
  136. Article int64
  137. SpaceTop int64
  138. }
  139. // Property .
  140. type Property struct {
  141. QsNum int
  142. PerScore int64
  143. }
  144. func init() {
  145. flag.StringVar(&confPath, "conf", "", "default config path")
  146. }
  147. // Init create config instance.
  148. func Init() (err error) {
  149. if confPath != "" {
  150. return local()
  151. }
  152. return remote()
  153. }
  154. func local() (err error) {
  155. _, err = toml.DecodeFile(confPath, &Conf)
  156. return
  157. }
  158. func remote() (err error) {
  159. if client, err = conf.New(); err != nil {
  160. return
  161. }
  162. if err = load(); err != nil {
  163. return
  164. }
  165. go func() {
  166. for range client.Event() {
  167. log.Info("config reload")
  168. if load() != nil {
  169. log.Error("config reload error (%v)", err)
  170. }
  171. }
  172. }()
  173. return
  174. }
  175. func load() (err error) {
  176. var (
  177. s string
  178. ok bool
  179. tmpConf *Config
  180. )
  181. if s, ok = client.Toml2(); !ok {
  182. return errors.New("load config center error")
  183. }
  184. if _, err = toml.Decode(s, &tmpConf); err != nil {
  185. return errors.New("could not decode config")
  186. }
  187. *Conf = *tmpConf
  188. return
  189. }