conf.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/queue/databus"
  13. "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // Conf global variable.
  17. var (
  18. Conf = &Config{}
  19. client *conf.Client
  20. confPath string
  21. )
  22. // Config struct of conf.
  23. type Config struct {
  24. // base
  25. // host
  26. Host *Host
  27. // Env
  28. Env string
  29. // goroutine sleep
  30. Tick time.Duration
  31. // log
  32. Xlog *log.Config
  33. // databus
  34. DataBus *DataBus
  35. // bm service
  36. BM *bm.ServerConfig
  37. // httpClinet
  38. HTTPClient *bm.ClientConfig
  39. Mysql *sql.Config
  40. Judge *Judge
  41. Sms *Sms
  42. Redis *Redis
  43. Memcache *Memcache
  44. // rpc client
  45. RPCClient *RPC
  46. }
  47. // RPC rpc client config.
  48. type RPC struct {
  49. Archive *rpc.ClientConfig
  50. Member *rpc.ClientConfig
  51. }
  52. // DataBus databus config.
  53. type DataBus struct {
  54. CreditDBSub *databus.Config
  55. ReplyAllSub *databus.Config
  56. LabourSub *databus.Config
  57. }
  58. // Host config host.
  59. type Host struct {
  60. APICoURI string
  61. AccountCoURI string
  62. MsgCoURI string
  63. }
  64. // Redis redis conf.
  65. type Redis struct {
  66. *redis.Config
  67. Expire time.Duration
  68. }
  69. // Sms is sms monitor config.
  70. type Sms struct {
  71. Phone string
  72. Token string
  73. }
  74. // Judge is judge config.
  75. type Judge struct {
  76. ConfTimer time.Duration // 定时load数据时间间隔
  77. ReservedTime time.Duration // 结案前N分钟停止获取case
  78. CaseGiveHours int64 // 案件发放时长
  79. CaseCheckTime int64 // 单案审核时长
  80. JuryRatio int64 // 投准率下限
  81. JudgeRadio int64 // 判决阙值
  82. CaseVoteMin int64 // 案件投票数下限
  83. CaseObtainMax int64 // 每日获取案件数
  84. CaseVoteMax int64 // 结案投票数
  85. JuryApplyMax int64 // 每日发放风纪委上限
  86. CaseLoadMax int // 案件发放最大队列数
  87. CaseLoadSwitch int8 // 案件发放进入队列开关
  88. CaseVoteMaxPercent int // 结案投票数的百分比
  89. VoteNum
  90. }
  91. // VoteNum struct.
  92. type VoteNum struct {
  93. RateS int8 `json:"rate_s"`
  94. RateA int8 `json:"rate_a"`
  95. RateB int8 `json:"rate_b"`
  96. RateC int8 `json:"rate_c"`
  97. RateD int8 `json:"rate_d"`
  98. }
  99. // Memcache memcache.
  100. type Memcache struct {
  101. *memcache.Config
  102. Expire time.Duration
  103. }
  104. func init() {
  105. flag.StringVar(&confPath, "conf", "", "default config path")
  106. }
  107. // Init create config instance.
  108. func Init() (err error) {
  109. if confPath != "" {
  110. return local()
  111. }
  112. return remote()
  113. }
  114. func local() (err error) {
  115. _, err = toml.DecodeFile(confPath, &Conf)
  116. return
  117. }
  118. func remote() (err error) {
  119. if client, err = conf.New(); err != nil {
  120. return
  121. }
  122. if err = load(); err != nil {
  123. return
  124. }
  125. go func() {
  126. for range client.Event() {
  127. log.Info("config reload")
  128. if load() != nil {
  129. log.Error("config reload error (%v)", err)
  130. }
  131. }
  132. }()
  133. return
  134. }
  135. func load() (err error) {
  136. var (
  137. s string
  138. ok bool
  139. tmpConf *Config
  140. )
  141. if s, ok = client.Toml2(); !ok {
  142. return errors.New("load config center error")
  143. }
  144. if _, err = toml.Decode(s, &tmpConf); err != nil {
  145. return errors.New("could not decode config")
  146. }
  147. *Conf = *tmpConf
  148. return
  149. }