conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. "go-common/library/database/hbase.v2"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config .
  23. type Config struct {
  24. Log *log.Config
  25. BM *bm.ServerConfig
  26. Tracer *trace.Config
  27. Ecode *ecode.Config
  28. AccountSummaryHBase *hbase.Config
  29. MemberBinLog *databus.Config
  30. BlockBinLog *databus.Config
  31. PassportBinLog *databus.Config
  32. RelationBinLog *databus.Config
  33. AccountSummaryProducer *databus.Config
  34. MemberService *rpc.ClientConfig
  35. RelationService *rpc.ClientConfig
  36. HTTPClient *bm.ClientConfig
  37. Host *Host
  38. FeatureGate *FeatureGate
  39. AccountSummary *AccountSummary
  40. MemberDB *sql.Config
  41. RelationDB *sql.Config
  42. PassportDB *sql.Config
  43. }
  44. // AccountSummary is
  45. type AccountSummary struct {
  46. SubProcessWorker uint64
  47. SyncRangeStart int64
  48. SyncRangeEnd int64
  49. SyncRangeWorker uint64
  50. InitialWriteWorker uint64
  51. }
  52. // FeatureGate is
  53. type FeatureGate struct {
  54. DisableSubProcess bool
  55. Initial bool
  56. InitialMemberBase bool
  57. InitialMemberExp bool
  58. InitialMemberOfficial bool
  59. InitialRelationStat bool
  60. InitialBlock bool
  61. InitialPassport bool
  62. SyncRange bool
  63. }
  64. // Host is
  65. type Host struct {
  66. Passport string
  67. }
  68. func init() {
  69. flag.StringVar(&confPath, "conf", "", "default config path")
  70. }
  71. // Init init conf
  72. func Init() 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. }