conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/trace"
  10. "go-common/library/queue/databus"
  11. "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. // Conf conf.
  17. Conf = &Config{}
  18. client *conf.Client
  19. )
  20. // Config config.
  21. type Config struct {
  22. // common conf
  23. // log
  24. Xlog *log.Config
  25. // Tracer tracer
  26. Tracer *trace.Config
  27. // http
  28. BM *BM
  29. // Database
  30. DB *DB
  31. // Step
  32. StepGroup *StepGroup
  33. // DataSwitch
  34. DataSwitch *DataSwitch
  35. // Group group
  36. Group *Group
  37. //Databus databus
  38. DataBus *DataBus
  39. }
  40. // BM http server config
  41. type BM struct {
  42. Inner *bm.ServerConfig
  43. Local *bm.ServerConfig
  44. }
  45. // DB db config
  46. type DB struct {
  47. OriginDB *sql.Config
  48. EncryptDB *sql.Config
  49. }
  50. // Group multi group config collection.
  51. type Group struct {
  52. AsoBinLog *GroupConfig
  53. }
  54. // GroupConfig group config.
  55. type GroupConfig struct {
  56. // Size merge size
  57. Size int
  58. // Num merge goroutine num
  59. Num int
  60. // Ticker duration of submit merges when no new message
  61. Ticker time.Duration
  62. // Chan size of merge chan and done chan
  63. Chan int
  64. }
  65. // DataBus databus.
  66. type DataBus struct {
  67. AsoBinLogSub *databus.Config
  68. }
  69. // StepGroup group
  70. type StepGroup struct {
  71. Group1 *Step
  72. Group2 *Step
  73. Group3 *Step
  74. Group4 *Step
  75. }
  76. // Step data step
  77. type Step struct {
  78. Start, End, Inc, Limit int64
  79. }
  80. // DataSwitch data trans swtich
  81. type DataSwitch struct {
  82. Full, Inc bool
  83. }
  84. func init() {
  85. flag.StringVar(&confPath, "conf", "", "default config path")
  86. }
  87. // Init init config.
  88. func Init() (err error) {
  89. if confPath != "" {
  90. return local()
  91. }
  92. return remote()
  93. }
  94. func local() (err error) {
  95. _, err = toml.DecodeFile(confPath, &Conf)
  96. return
  97. }
  98. func remote() (err error) {
  99. if client, err = conf.New(); err != nil {
  100. return
  101. }
  102. if err = load(); err != nil {
  103. return
  104. }
  105. go func() {
  106. for range client.Event() {
  107. log.Info("config reload")
  108. if load() != nil {
  109. log.Error("config reload error (%v)", err)
  110. }
  111. }
  112. }()
  113. return
  114. }
  115. func load() (err error) {
  116. var (
  117. s string
  118. ok bool
  119. tmpConf = &Config{}
  120. )
  121. if s, ok = client.Toml2(); !ok {
  122. return errors.New("load config center error")
  123. }
  124. if _, err = toml.Decode(s, tmpConf); err != nil {
  125. return errors.New("could not decode config")
  126. }
  127. *Conf = *tmpConf
  128. return
  129. }