conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/trace"
  11. "go-common/library/queue/databus"
  12. "go-common/library/queue/databus/databusutil"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. // Conf conf.
  19. Conf = &Config{}
  20. client *conf.Client
  21. )
  22. // Config config.
  23. type Config struct {
  24. // log
  25. Xlog *log.Config
  26. // Tracer tracer
  27. Tracer *trace.Config
  28. // http
  29. BM *bm.ServerConfig
  30. // Database
  31. DB *DB
  32. // mc
  33. Memcache *Memcache
  34. //Databus databus
  35. DataBus *DataBus
  36. // DataUtil config
  37. DatabusUtil *databusutil.Config
  38. // IncSync
  39. IncSync *IncSync
  40. // FullSync
  41. FullSync *FullSync
  42. // Scheduler
  43. Scheduler *Scheduler
  44. }
  45. // DB db config
  46. type DB struct {
  47. OriginDB *sql.Config
  48. UserDB *sql.Config
  49. EncryptDB *sql.Config
  50. }
  51. // Memcache memcache
  52. type Memcache struct {
  53. *memcache.Config
  54. Expire xtime.Duration
  55. }
  56. // DataBus databus.
  57. type DataBus struct {
  58. AsoBinLogSub *databus.Config
  59. }
  60. // IncSync increment sync
  61. type IncSync struct {
  62. Switch bool
  63. }
  64. // FullSync full sync
  65. type FullSync struct {
  66. AsoCountryCodeSwitch bool
  67. AsoAccount *SyncConf
  68. AsoAccountInfo *SyncConf
  69. AsoAccountReg *SyncConf
  70. AsoAccountSns *SyncConf
  71. AsoTelBindLog *SyncConf
  72. }
  73. // SyncConf sync conf
  74. type SyncConf struct {
  75. Switch bool
  76. ChanNum int
  77. ChanSize int
  78. Start int64
  79. End int64
  80. Count int64
  81. }
  82. // Scheduler scheduler
  83. type Scheduler struct {
  84. Switch bool
  85. TelDuplicateCron string
  86. EmailDuplicateCron string
  87. }
  88. func init() {
  89. flag.StringVar(&confPath, "conf", "", "default config path")
  90. }
  91. // Init init config.
  92. func Init() (err error) {
  93. if confPath != "" {
  94. return local()
  95. }
  96. return remote()
  97. }
  98. func local() (err error) {
  99. _, err = toml.DecodeFile(confPath, &Conf)
  100. return
  101. }
  102. func remote() (err error) {
  103. if client, err = conf.New(); err != nil {
  104. return
  105. }
  106. if err = load(); err != nil {
  107. return
  108. }
  109. go func() {
  110. for range client.Event() {
  111. log.Info("config reload")
  112. if load() != nil {
  113. log.Error("config reload error (%v)", err)
  114. }
  115. }
  116. }()
  117. return
  118. }
  119. func load() (err error) {
  120. var (
  121. s string
  122. ok bool
  123. tmpConf = &Config{}
  124. )
  125. if s, ok = client.Toml2(); !ok {
  126. return errors.New("load config center error")
  127. }
  128. if _, err = toml.Decode(s, tmpConf); err != nil {
  129. return errors.New("could not decode config")
  130. }
  131. *Conf = *tmpConf
  132. return
  133. }