conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. // config
  16. confPath string
  17. client *conf.Client
  18. // Conf config
  19. Conf = &Config{}
  20. )
  21. // Config def.
  22. type Config struct {
  23. // base
  24. // db
  25. DB *DB
  26. // spy rpc client
  27. SpyRPC *rpc.ClientConfig
  28. AccountRPC *rpc.ClientConfig
  29. // memcache
  30. Memcache *Memcache
  31. // log
  32. Log *log.Config
  33. // customized property
  34. Property *Property
  35. HTTPServer *bm.ServerConfig
  36. }
  37. // DB config.
  38. type DB struct {
  39. Spy *sql.Config
  40. }
  41. // Memcache config.
  42. type Memcache struct {
  43. User *memcache.Config
  44. UserExpire time.Duration
  45. }
  46. // Property config for biz logic.
  47. type Property struct {
  48. TelValidateURL string
  49. BlockAccountURL string
  50. UserInfoShard int64
  51. HistoryShard int64
  52. LoadEventTick time.Duration
  53. Score *struct {
  54. BaseInit int8
  55. EventInit int8
  56. }
  57. Punishment *struct {
  58. ScoreThreshold int8
  59. Times int8
  60. }
  61. Event *struct {
  62. ServiceName string
  63. BindMailAndTelLowRisk string
  64. BindMailOnly string
  65. BindNothing string
  66. BindTelLowRiskOnly string
  67. BindTelMediumRisk string
  68. BindTelHighRisk string
  69. BindTelUnknownRisk string
  70. }
  71. // activity events
  72. ActivityEvents map[int32]struct{}
  73. }
  74. func local() (err error) {
  75. _, err = toml.DecodeFile(confPath, &Conf)
  76. return
  77. }
  78. func remote() (err error) {
  79. if client, err = conf.New(); err != nil {
  80. return
  81. }
  82. if err = load(); err != nil {
  83. return
  84. }
  85. go func() {
  86. for range client.Event() {
  87. log.Info("config reload")
  88. if load() != nil {
  89. log.Error("config reload error (%v)", err)
  90. }
  91. }
  92. }()
  93. return
  94. }
  95. func load() (err error) {
  96. var (
  97. s string
  98. ok bool
  99. tmpConf *Config
  100. )
  101. if s, ok = client.Toml2(); !ok {
  102. return errors.New("load config center error")
  103. }
  104. if _, err = toml.Decode(s, &tmpConf); err != nil {
  105. return errors.New("could not decode config")
  106. }
  107. *Conf = *tmpConf
  108. return
  109. }
  110. func init() {
  111. flag.StringVar(&confPath, "conf", "", "default config path")
  112. }
  113. // Init int config
  114. func Init() error {
  115. if confPath != "" {
  116. return local()
  117. }
  118. return remote()
  119. }