conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/rpc/warden"
  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. // 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. OfflineMySQL *sql.Config
  32. CmsMySQL *sql.Config
  33. Ecode *ecode.Config
  34. Berserker *BerserkerConfig
  35. Job *Jobs
  36. GRPCClient map[string]*GRPCConfig
  37. }
  38. // Jobs .
  39. type Jobs struct {
  40. ForwardIndex *JobConfig
  41. BloomFilter *JobConfig
  42. }
  43. // JobConfig .
  44. type JobConfig struct {
  45. JobName string
  46. Schedule string
  47. Input string
  48. Output string
  49. }
  50. // GRPCConfig .
  51. type GRPCConfig struct {
  52. WardenConf *warden.ClientConfig
  53. Addr string
  54. }
  55. func init() {
  56. flag.StringVar(&confPath, "conf", "", "default config path")
  57. }
  58. // Init init conf
  59. func Init() error {
  60. if confPath != "" {
  61. return local()
  62. }
  63. return remote()
  64. }
  65. func local() (err error) {
  66. _, err = toml.DecodeFile(confPath, &Conf)
  67. return
  68. }
  69. func remote() (err error) {
  70. if client, err = conf.New(); err != nil {
  71. return
  72. }
  73. if err = load(); err != nil {
  74. return
  75. }
  76. go func() {
  77. for range client.Event() {
  78. log.Info("config reload")
  79. if load() != nil {
  80. log.Error("config reload error (%v)", err)
  81. }
  82. }
  83. }()
  84. return
  85. }
  86. func load() (err error) {
  87. var (
  88. s string
  89. ok bool
  90. tmpConf *Config
  91. )
  92. if s, ok = client.Toml2(); !ok {
  93. return errors.New("load config center error")
  94. }
  95. if _, err = toml.Decode(s, &tmpConf); err != nil {
  96. return errors.New("could not decode config")
  97. }
  98. *Conf = *tmpConf
  99. return
  100. }