conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. "go-common/library/queue/databus"
  15. "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 *blademaster.ServerConfig
  28. GRPC *warden.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *redis.Config
  32. RedisExpire *RedisExpire
  33. MySQL *sql.Config
  34. Ecode *ecode.Config
  35. Databus *Databus
  36. MidMapping map[string]int
  37. OidWhiteList map[string]int
  38. }
  39. // RedisExpire RedisExpire
  40. type RedisExpire struct {
  41. RedisReplyZSetExpire time.Duration
  42. }
  43. // Databus databus
  44. type Databus struct {
  45. Event *databus.Config
  46. }
  47. func init() {
  48. flag.StringVar(&confPath, "conf", "", "default config path")
  49. }
  50. // Init init conf
  51. func Init() error {
  52. if confPath != "" {
  53. return local()
  54. }
  55. return remote()
  56. }
  57. func local() (err error) {
  58. _, err = toml.DecodeFile(confPath, &Conf)
  59. return
  60. }
  61. func remote() (err error) {
  62. if client, err = conf.New(); err != nil {
  63. return
  64. }
  65. if err = load(); err != nil {
  66. return
  67. }
  68. go func() {
  69. for range client.Event() {
  70. log.Info("config reload")
  71. if load() != nil {
  72. log.Error("config reload error (%v)", err)
  73. }
  74. }
  75. }()
  76. return
  77. }
  78. func load() (err error) {
  79. var (
  80. s string
  81. ok bool
  82. tmpConf *Config
  83. )
  84. if s, ok = client.Toml2(); !ok {
  85. return errors.New("load config center error")
  86. }
  87. if _, err = toml.Decode(s, &tmpConf); err != nil {
  88. return errors.New("could not decode config")
  89. }
  90. *Conf = *tmpConf
  91. return
  92. }