conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  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. // log
  23. Log *log.Config
  24. // Tracer tracer
  25. Tracer *trace.Config
  26. // DB db
  27. DB *DB
  28. // BM
  29. BM *httpx.ServerConfig
  30. // FullTask
  31. FullTask *FullTask
  32. // IncTask
  33. IncTask *IncTask
  34. // WeChat
  35. WeChat *WeChat
  36. // data fix switch
  37. DataFixSwitch bool
  38. IncrDataFixSwitch bool
  39. // httpClient
  40. HTTPClient *bm.ClientConfig
  41. // DuplicateTask
  42. DuplicateTask *DuplicateTask
  43. FixEmailVerifiedSwitch bool
  44. }
  45. // FullTask full task compare
  46. type FullTask struct {
  47. Switch bool
  48. Step int64
  49. AccountEnd int64
  50. AccountInfoEnd int64
  51. AccountSnsEnd int64
  52. CronFullStr string
  53. }
  54. // WeChat wechat basic info
  55. type WeChat struct {
  56. Token string
  57. Secret string
  58. Username string
  59. }
  60. // IncTask incr task compare
  61. type IncTask struct {
  62. Switch bool
  63. StartTime string
  64. StepDuration time.Duration
  65. CronIncStr string
  66. }
  67. // DB db config.
  68. type DB struct {
  69. User *sql.Config
  70. Origin *sql.Config
  71. Secret *sql.Config
  72. }
  73. // DuplicateTask check duplicatet ask
  74. type DuplicateTask struct {
  75. Switch bool
  76. DuplicateCron string
  77. }
  78. func init() {
  79. flag.StringVar(&confPath, "conf", "", "default config path")
  80. }
  81. // Init init config.
  82. func Init() (err error) {
  83. if confPath != "" {
  84. return local()
  85. }
  86. return remote()
  87. }
  88. func local() (err error) {
  89. _, err = toml.DecodeFile(confPath, &Conf)
  90. return
  91. }
  92. func remote() (err error) {
  93. if client, err = conf.New(); err != nil {
  94. return
  95. }
  96. if err = load(); err != nil {
  97. return
  98. }
  99. go func() {
  100. for range client.Event() {
  101. log.Info("config reload")
  102. if load() != nil {
  103. log.Error("config reload error (%v)", err)
  104. }
  105. }
  106. }()
  107. return
  108. }
  109. func load() (err error) {
  110. var (
  111. s string
  112. ok bool
  113. tmpConf *Config
  114. )
  115. if s, ok = client.Toml2(); !ok {
  116. return errors.New("load config center error")
  117. }
  118. if _, err = toml.Decode(s, &tmpConf); err != nil {
  119. return errors.New("could not decode config")
  120. }
  121. *Conf = *tmpConf
  122. return
  123. }