conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/queue/databus"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. // Conf .
  20. Conf = &Config{}
  21. confPath string
  22. client *conf.Client
  23. )
  24. // Config struct
  25. type Config struct {
  26. Xlog *log.Config
  27. Tracer *trace.Config
  28. BM *bm.ServerConfig
  29. DB *sql.Config
  30. Ecode *ecode.Config
  31. Verify *verify.Config
  32. Redis *redis.Config
  33. AllowType []int
  34. RedisExpire int64
  35. Databus *databus.Config
  36. ArchiveDatabus *databus.Config
  37. WardenServer *warden.ServerConfig
  38. ArcRPC *rpc.ClientConfig
  39. Target int64
  40. Sources []int64
  41. Worker int
  42. }
  43. func init() {
  44. flag.StringVar(&confPath, "conf", "", "default config path")
  45. }
  46. // Init init config.
  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. }