conf.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/trace"
  9. "go-common/library/queue/databus"
  10. "go-common/library/time"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. confPath string
  15. // Conf conf.
  16. Conf = &Config{}
  17. client *conf.Client
  18. )
  19. // Config config.
  20. type Config struct {
  21. // log
  22. Xlog *log.Config
  23. // Tracer tracer
  24. Tracer *trace.Config
  25. // HTTP
  26. BM *bm.ServerConfig
  27. // Group group
  28. Group *Group
  29. //Databus databus
  30. DataBus *DataBus
  31. }
  32. // Group multi group config collection.
  33. type Group struct {
  34. AsoBinLog *GroupConfig
  35. }
  36. // GroupConfig group config.
  37. type GroupConfig struct {
  38. // Size merge size
  39. Size int
  40. // Num merge goroutine num
  41. Num int
  42. // Ticker duration of submit merges when no new message
  43. Ticker time.Duration
  44. // Chan size of merge chan and done chan
  45. Chan int
  46. }
  47. // DataBus databus.
  48. type DataBus struct {
  49. AsoBinLogSub *databus.Config
  50. EncryptTransPub *databus.Config
  51. }
  52. func init() {
  53. flag.StringVar(&confPath, "conf", "", "default config path")
  54. }
  55. // Init init config.
  56. func Init() (err error) {
  57. if confPath != "" {
  58. return local()
  59. }
  60. return remote()
  61. }
  62. func local() (err error) {
  63. _, err = toml.DecodeFile(confPath, &Conf)
  64. return
  65. }
  66. func remote() (err error) {
  67. if client, err = conf.New(); err != nil {
  68. return
  69. }
  70. if err = load(); err != nil {
  71. return
  72. }
  73. go func() {
  74. for range client.Event() {
  75. log.Info("config reload")
  76. if load() != nil {
  77. log.Error("config reload error (%v)", err)
  78. }
  79. }
  80. }()
  81. return
  82. }
  83. func load() (err error) {
  84. var (
  85. s string
  86. ok bool
  87. tmpConf *Config
  88. )
  89. if s, ok = client.Toml2(); !ok {
  90. return errors.New("load config center error")
  91. }
  92. if _, err = toml.Decode(s, &tmpConf); err != nil {
  93. return errors.New("could not decode config")
  94. }
  95. *Conf = *tmpConf
  96. return
  97. }