conf.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/trace"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Verify *verify.Config
  27. Tracer *trace.Config
  28. Redis *redis.Config
  29. BFRedis *redis.Config
  30. MySQL *sql.Config
  31. Ecode *ecode.Config
  32. WorkPool *WorkPoolConfig
  33. ForwardIndex *ForwardIndexConfig
  34. LocalCache *LocalCacheConfig
  35. }
  36. // WorkPoolConfig .
  37. type WorkPoolConfig struct {
  38. Capacity uint64
  39. MaxWorkers uint64
  40. MaxIdleWorkers uint64
  41. MinIdleWorkers uint64
  42. KeepAlive xtime.Duration
  43. }
  44. // ForwardIndexConfig .
  45. type ForwardIndexConfig struct {
  46. LocalPath string
  47. RemotePath string
  48. MD5Path string
  49. Protocol string
  50. ReloadDucation xtime.Duration
  51. }
  52. // LocalCacheConfig .
  53. type LocalCacheConfig struct {
  54. L1Tags []string
  55. Level1 xtime.Duration
  56. L2Tags []string
  57. Level2 xtime.Duration
  58. Level3 xtime.Duration
  59. MaxAge xtime.Duration
  60. }
  61. func init() {
  62. flag.StringVar(&confPath, "conf", "", "default config path")
  63. }
  64. // Init init conf
  65. func Init() error {
  66. if confPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(confPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Toml2(); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }