conf.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/http/blademaster/middleware/auth"
  11. "go-common/library/net/trace"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf conf
  19. Conf = &Config{}
  20. )
  21. // Config config
  22. type Config struct {
  23. Rule *Rule
  24. XLog *log.Config
  25. Tracer *trace.Config
  26. Auth *auth.Config
  27. // memcache
  28. Memcache *KvoMemcache
  29. // mysql
  30. Mysql *sql.Config
  31. // bm
  32. BM *bm.ServerConfig
  33. }
  34. // Rule .
  35. type Rule struct {
  36. DocLimit int
  37. }
  38. // KvoMemcache memcache config
  39. type KvoMemcache struct {
  40. Kvo *memcache.Config
  41. Expire time.Duration
  42. }
  43. func init() {
  44. flag.StringVar(&confPath, "conf", "", "config path")
  45. }
  46. // Init init conf
  47. func Init() (err error) {
  48. if confPath != "" {
  49. _, err = toml.DecodeFile(confPath, &Conf)
  50. return
  51. }
  52. err = remote()
  53. return
  54. }
  55. func remote() (err error) {
  56. if client, err = conf.New(); err != nil {
  57. return
  58. }
  59. if err = load(); err != nil {
  60. return
  61. }
  62. go func() {
  63. for range client.Event() {
  64. log.Info("config reload")
  65. if load() != nil {
  66. log.Error("config reload error (%v)", err)
  67. }
  68. }
  69. }()
  70. return
  71. }
  72. func load() (err error) {
  73. var (
  74. s string
  75. ok bool
  76. tmpConf *Config
  77. )
  78. if s, ok = client.Toml2(); !ok {
  79. return errors.New("load config center error")
  80. }
  81. if _, err = toml.Decode(s, &tmpConf); err != nil {
  82. return errors.New("could not decode config")
  83. }
  84. *Conf = *tmpConf
  85. return
  86. }