conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config .
  25. type Config struct {
  26. Log *log.Config
  27. BM *bm.ServerConfig
  28. Verify *verify.Config
  29. Tracer *trace.Config
  30. Redis *redis.Config
  31. RedisExpire *RedisExpire
  32. Memcache *memcache.Config
  33. MemcacheExpire *MemcacheExpire
  34. MySQL *MySQL
  35. Databus *Databus
  36. Ecode *ecode.Config
  37. RefreshTime int64
  38. HTTPClient *bm.ClientConfig
  39. }
  40. // RedisExpire Redis
  41. type RedisExpire struct {
  42. RedisReplySetExpire xtime.Duration
  43. RedisReplyZSetExpire xtime.Duration
  44. RedisRefreshExpire xtime.Duration
  45. }
  46. // MemcacheExpire Memcache
  47. type MemcacheExpire struct {
  48. McStatExpire xtime.Duration
  49. }
  50. // Databus databus
  51. type Databus struct {
  52. Stats *databus.Config
  53. Event *databus.Config
  54. }
  55. // MySQL mysql config
  56. type MySQL struct {
  57. DB *sql.Config
  58. DBSlave *sql.Config
  59. }
  60. func init() {
  61. flag.StringVar(&confPath, "conf", "", "default config path")
  62. }
  63. // Init init conf
  64. func Init() (err error) {
  65. if confPath != "" {
  66. return local()
  67. }
  68. if err = remote(); err != nil {
  69. return
  70. }
  71. if Conf.RefreshTime <= 0 {
  72. panic("refresh time illegal.")
  73. }
  74. return
  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. }