conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/log/infoc"
  6. "go-common/library/queue/databus"
  7. "go-common/library/cache/memcache"
  8. "go-common/library/cache/redis"
  9. "go-common/library/conf"
  10. "go-common/library/database/sql"
  11. ecode "go-common/library/ecode/tip"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/trace"
  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. Memcache *memcache.Config
  32. MySQL *sql.Config
  33. Ecode *ecode.Config
  34. Databus *Databus
  35. Infoc map[string]*infoc.Config
  36. Consumer *consumer
  37. }
  38. type consumer struct {
  39. AddGift *consumeConfig
  40. }
  41. type consumeConfig struct {
  42. Num int
  43. }
  44. //Databus Databus
  45. type Databus struct {
  46. AddGift *databus.Config
  47. }
  48. func init() {
  49. flag.StringVar(&confPath, "conf", "", "default config path")
  50. }
  51. // Init init conf
  52. func Init() error {
  53. if confPath != "" {
  54. return local()
  55. }
  56. return remote()
  57. }
  58. func local() (err error) {
  59. _, err = toml.DecodeFile(confPath, &Conf)
  60. return
  61. }
  62. func remote() (err error) {
  63. if client, err = conf.New(); err != nil {
  64. return
  65. }
  66. if err = load(); err != nil {
  67. return
  68. }
  69. go func() {
  70. for range client.Event() {
  71. log.Info("config reload")
  72. if load() != nil {
  73. log.Error("config reload error (%v)", err)
  74. }
  75. }
  76. }()
  77. return
  78. }
  79. func load() (err error) {
  80. var (
  81. s string
  82. ok bool
  83. tmpConf *Config
  84. )
  85. if s, ok = client.Toml2(); !ok {
  86. return errors.New("load config center error")
  87. }
  88. if _, err = toml.Decode(s, &tmpConf); err != nil {
  89. return errors.New("could not decode config")
  90. }
  91. *Conf = *tmpConf
  92. return
  93. }