conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "go-common/library/queue/databus"
  17. "go-common/library/time"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *redis.Config
  32. Memcache *memcache.Config
  33. MySQL *sql.Config
  34. Ecode *ecode.Config
  35. LiveRpc map[string]*liverpc.ClientConfig
  36. DataBus *DataBus
  37. GeeTest *GeeTestConfig
  38. LogStream *LogStream
  39. }
  40. // DataBus ...
  41. type DataBus struct {
  42. CaptchaAnti *databus.Config
  43. }
  44. // GeeTestConfig ...
  45. type GeeTestConfig struct {
  46. On int64 //是否开启极验
  47. Id string //公钥
  48. Key string //秘钥
  49. Qps int64 //限制qps
  50. Slice int64 //限制qps的key分几份
  51. Get HttpMethod //get配置
  52. Post HttpMethod //post配置
  53. }
  54. // HttpMethod config
  55. type HttpMethod struct {
  56. Timeout int64
  57. KeepAlive int64
  58. }
  59. // LogStream config
  60. type LogStream struct {
  61. LogId string
  62. Token string
  63. Address string
  64. Capacity int
  65. Timeout time.Duration
  66. }
  67. // init
  68. func init() {
  69. flag.StringVar(&confPath, "conf", "", "default config path")
  70. }
  71. // Init init conf
  72. func Init() error {
  73. if confPath != "" {
  74. return local()
  75. }
  76. return remote()
  77. }
  78. func local() (err error) {
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. log.Info("config reload")
  92. if load() != nil {
  93. log.Error("config reload error (%v)", err)
  94. }
  95. }
  96. }()
  97. return
  98. }
  99. func load() (err error) {
  100. var (
  101. s string
  102. ok bool
  103. tmpConf *Config
  104. )
  105. if s, ok = client.Toml2(); !ok {
  106. return errors.New("load config center error")
  107. }
  108. if _, err = toml.Decode(s, &tmpConf); err != nil {
  109. return errors.New("could not decode config")
  110. }
  111. *Conf = *tmpConf
  112. return
  113. }