conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. httpx "go-common/library/net/http/blademaster"
  9. "go-common/library/net/trace"
  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. // DB db
  26. DB *DB
  27. // Compare compare
  28. Compare *Compare
  29. // InitCloud init cloud.
  30. InitCloud *InitCloud
  31. // BM
  32. BM *httpx.ServerConfig
  33. }
  34. // InitCloud init cloud conf.
  35. type InitCloud struct {
  36. OffsetFilePath string
  37. UseOldOffset bool
  38. Start, End int64
  39. Batch int
  40. Sleep time.Duration
  41. }
  42. // Compare compare
  43. type Compare struct {
  44. Cloud2Local *CompareConfig
  45. Local2Cloud *CompareConfig
  46. }
  47. // CompareConfig compare proc config.
  48. type CompareConfig struct {
  49. On bool
  50. Debug bool
  51. OffsetFilePath string
  52. UseOldOffset bool
  53. End bool
  54. StartTime string
  55. EndTime string
  56. DelayDuration time.Duration
  57. StepDuration time.Duration
  58. LoopDuration time.Duration
  59. BatchSize int
  60. BatchMissRetryCount int
  61. Fix bool
  62. }
  63. // DB db config.
  64. type DB struct {
  65. Local *sql.Config
  66. Cloud *sql.Config
  67. }
  68. func init() {
  69. flag.StringVar(&confPath, "conf", "", "default config path")
  70. }
  71. // Init init config.
  72. func Init() (err error) {
  73. if confPath != "" {
  74. return local()
  75. }
  76. return remote()
  77. }
  78. func local() (err error) {
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. log.Info("config reload")
  92. if load() != nil {
  93. log.Error("config reload error (%v)", err)
  94. }
  95. }
  96. }()
  97. return
  98. }
  99. func load() (err error) {
  100. var (
  101. s string
  102. ok bool
  103. tmpConf *Config
  104. )
  105. if s, ok = client.Toml2(); !ok {
  106. return errors.New("load config center error")
  107. }
  108. if _, err = toml.Decode(s, &tmpConf); err != nil {
  109. return errors.New("could not decode config")
  110. }
  111. *Conf = *tmpConf
  112. return
  113. }