conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "github.com/BurntSushi/toml"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  10. ecode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/rpc/liverpc"
  15. "go-common/library/net/trace"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config .
  24. type Config struct {
  25. Log *log.Config
  26. BM *bm.ServerConfig
  27. Verify *verify.Config
  28. Tracer *trace.Config
  29. Redis *redis.Config
  30. Memcache *memcache.Config
  31. MySQL *sql.Config
  32. Ecode *ecode.Config
  33. LiveRpc map[string]*liverpc.ClientConfig
  34. Feature *FeatureConf
  35. CommonFeature *CommonFeatureConf
  36. }
  37. // CommonFeatureConf 细分维度的特征权重配置
  38. type CommonFeatureConf struct {
  39. UserAreaInterest NumMatch
  40. FansNum RangeSplit
  41. CornerSign ReMatch
  42. Online RangeSplit
  43. }
  44. // NumMatch 通过int匹配获得权重
  45. type NumMatch struct {
  46. Type string
  47. Values []int
  48. Weights []float32
  49. }
  50. // ReMatch 通过文字匹配获得权重
  51. type ReMatch struct {
  52. Type string
  53. Values []string
  54. Weights []float32
  55. }
  56. // RangeSplit 通过分割区间获得权重
  57. type RangeSplit struct {
  58. Type string
  59. Values []int64
  60. Weights []float32
  61. }
  62. // FeatureConf 特征配置
  63. type FeatureConf struct {
  64. WeightVector []float32
  65. }
  66. func init() {
  67. flag.StringVar(&confPath, "conf", "", "default config path")
  68. }
  69. // Init init conf
  70. func Init() error {
  71. if confPath != "" {
  72. return local()
  73. }
  74. return remote()
  75. }
  76. func local() (err error) {
  77. _, err = toml.DecodeFile(confPath, &Conf)
  78. return
  79. }
  80. func remote() (err error) {
  81. if client, err = conf.New(); err != nil {
  82. return
  83. }
  84. if err = load(); err != nil {
  85. return
  86. }
  87. go func() {
  88. for range client.Event() {
  89. log.Info("config reload")
  90. if load() != nil {
  91. log.Error("config reload error (%v)", err)
  92. }
  93. }
  94. }()
  95. return
  96. }
  97. func load() (err error) {
  98. var (
  99. s string
  100. ok bool
  101. tmpConf *Config
  102. )
  103. if s, ok = client.Toml2(); !ok {
  104. return errors.New("load config center error")
  105. }
  106. if _, err = toml.Decode(s, &tmpConf); err != nil {
  107. return errors.New("could not decode config")
  108. }
  109. *Conf = *tmpConf
  110. return
  111. }