conf.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "path"
  8. "strings"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/cache/redis"
  11. "go-common/library/conf"
  12. "go-common/library/database/orm"
  13. "go-common/library/log"
  14. "go-common/library/net/http/blademaster"
  15. bm "go-common/library/net/http/blademaster"
  16. "go-common/library/net/http/blademaster/middleware/permit"
  17. "go-common/library/net/rpc"
  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 info.
  24. var (
  25. ConfPath string
  26. Conf = &Config{}
  27. client *conf.Client
  28. CreditConfig = &CreditConf{}
  29. IsMaster = true
  30. )
  31. const (
  32. //ServiceName service name
  33. ServiceName = "upcredit-service"
  34. )
  35. // Config struct.
  36. type Config struct {
  37. // bm
  38. BM *HTTPServers
  39. // db
  40. DB *DB
  41. // base
  42. // elk
  43. Xlog *log.Config
  44. // report log
  45. LogCli *log.AgentConfig
  46. // httpClinet
  47. HTTPClient *HTTPClient
  48. // tracer
  49. Tracer *trace.Config
  50. // Redis
  51. Redis *Redis
  52. // rpc server
  53. RPCServer *rpc.ServerConfig
  54. // auth
  55. Auth *permit.Config
  56. IsTest bool
  57. CreditLogSub *databus.Config
  58. BusinessBinLogSub *databus.Config
  59. RunStatJobConf *RunStatJob
  60. MiscConf *MiscConfig
  61. ElectionZooKeeper *Zookeeper
  62. }
  63. //UpSub upsub config
  64. //type upSub struct {
  65. // *databus.Config
  66. // UpChanSize int
  67. // ConsumeLimit int
  68. // RoutineLimit int
  69. //}
  70. //MiscConfig other config set
  71. type MiscConfig struct {
  72. CreditLogWriteRoutineNum int
  73. BusinessBinLogLimitRate float64 // 每秒多少个,business bin log 消费速度
  74. }
  75. //HTTPServers for http server.
  76. type HTTPServers struct {
  77. Inner *blademaster.ServerConfig
  78. }
  79. // DB conf.
  80. type DB struct {
  81. Upcrm *orm.Config
  82. UpcrmReader *orm.Config
  83. }
  84. // Redis conf.
  85. type Redis struct {
  86. Databus *struct {
  87. *redis.Config
  88. Expire time.Duration
  89. }
  90. }
  91. // HTTPClient conf.
  92. type HTTPClient struct {
  93. Normal *bm.ClientConfig
  94. Slow *bm.ClientConfig
  95. }
  96. // Host conf.
  97. type Host struct {
  98. API string
  99. Live string
  100. Search string
  101. Manager string
  102. }
  103. // Monitor conf.
  104. type Monitor struct {
  105. Host string
  106. Moni string
  107. UserName string
  108. AppSecret string
  109. AppToken string
  110. IntervalAlarm time.Duration
  111. }
  112. //RunStatJob 定时任务时间
  113. type RunStatJob struct {
  114. // 启动时间,比如 12:00:00,每天定时运行
  115. StartTime string
  116. // 起的计算线程数
  117. WorkerNumber int
  118. }
  119. //App for key secret.
  120. type App struct {
  121. Key string
  122. Secret string
  123. }
  124. //MC memcache
  125. type MC struct {
  126. UpExpire time.Duration
  127. Up *memcache.Config
  128. }
  129. //CreditLog 需要记录日志的那些稿件状态,在配置文件中配置。只有这些状态,才会记录信用日志
  130. type CreditLog struct {
  131. NeedLogState map[int]CreditLogStateInfo
  132. }
  133. //CreditLogStateInfo nothing
  134. type CreditLogStateInfo struct {
  135. }
  136. // Zookeeper Server&Client settings.
  137. type Zookeeper struct {
  138. Root string
  139. Addrs []string
  140. Timeout time.Duration
  141. }
  142. func init() {
  143. flag.StringVar(&ConfPath, "conf", "", "default config path")
  144. }
  145. // Init conf.
  146. func Init() (err error) {
  147. if ConfPath != "" {
  148. return local()
  149. }
  150. return remote()
  151. }
  152. func local() (err error) {
  153. _, err = toml.DecodeFile(ConfPath, &Conf)
  154. if err != nil {
  155. return
  156. }
  157. ConfPath = strings.Replace(ConfPath, string(os.PathSeparator), "/", -1)
  158. var dir = path.Dir(ConfPath)
  159. var articleConfPath = path.Join(dir, "credit_score_conf.toml")
  160. _, err = toml.DecodeFile(articleConfPath, &CreditConfig)
  161. CreditConfig.AfterLoad()
  162. return
  163. }
  164. func remote() (err error) {
  165. if client, err = conf.New(); err != nil {
  166. return
  167. }
  168. if err = load(); err != nil {
  169. return
  170. }
  171. go func() {
  172. for range client.Event() {
  173. log.Info("config reload")
  174. if load() != nil {
  175. log.Error("config reload error (%v)", err)
  176. }
  177. }
  178. }()
  179. return
  180. }
  181. func load() (err error) {
  182. var (
  183. tomlStr string
  184. ok bool
  185. tmpConf *Config
  186. )
  187. if tomlStr, ok = client.Value("upcredit-service.toml"); !ok {
  188. return errors.New("load config center error")
  189. }
  190. if _, err = toml.Decode(tomlStr, &tmpConf); err != nil {
  191. return errors.New("could not decode toml config")
  192. }
  193. *Conf = *tmpConf
  194. fmt.Printf("loading credit_score_conf.toml from remoate...")
  195. if tomlStr, ok = client.Value("credit_score_conf.toml"); !ok {
  196. return errors.New("load config center error for credit_score_conf.toml")
  197. }
  198. var tmpConf2 *CreditConf
  199. if _, err = toml.Decode(tomlStr, &tmpConf2); err != nil {
  200. return errors.New("could not decode toml config for credit_score_conf.toml")
  201. }
  202. *CreditConfig = *tmpConf2
  203. CreditConfig.AfterLoad()
  204. return
  205. }