conf.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/database/sql"
  8. "go-common/library/conf"
  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. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. type IndexConfig struct {
  23. Matcher string
  24. }
  25. type SvenConfig struct {
  26. TreeID string
  27. Zone string
  28. Env string
  29. Build string
  30. Token string
  31. }
  32. // Config .
  33. type Config struct {
  34. Log *log.Config
  35. BM *bm.ServerConfig
  36. Verify *verify.Config
  37. Tracer *trace.Config
  38. Redis *redis.Config
  39. Memcache *memcache.Config
  40. MySQL *sql.Config
  41. Ecode *ecode.Config
  42. Sven *SvenConfig
  43. Index *IndexConfig
  44. }
  45. func init() {
  46. flag.StringVar(&confPath, "conf", "", "default config path")
  47. }
  48. // Init init conf
  49. func Init() error {
  50. if confPath != "" {
  51. return local()
  52. }
  53. return remote()
  54. }
  55. func local() (err error) {
  56. _, err = toml.DecodeFile(confPath, &Conf)
  57. return
  58. }
  59. func remote() (err error) {
  60. if client, err = conf.New(); err != nil {
  61. return
  62. }
  63. if err = load(); err != nil {
  64. return
  65. }
  66. go func() {
  67. for range client.Event() {
  68. // Stupid code !
  69. //log.Info("config reload")
  70. //if load() != nil {
  71. // log.Error("config reload error (%v)", err)
  72. //}
  73. }
  74. }()
  75. return
  76. }
  77. func load() (err error) {
  78. var (
  79. s string
  80. ok bool
  81. tmpConf *Config
  82. )
  83. if s, ok = client.Toml2(); !ok {
  84. return errors.New("load config center error")
  85. }
  86. if _, err = toml.Decode(s, &tmpConf); err != nil {
  87. return errors.New("could not decode config")
  88. }
  89. *Conf = *tmpConf
  90. return
  91. }