conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. client *conf.Client
  20. // Conf config
  21. Conf = &Config{}
  22. )
  23. // Config .
  24. type Config struct {
  25. Rank *Rank
  26. Log *log.Config
  27. BM *bm.ServerConfig
  28. RPCServer *rpc.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. MySQL *DB
  32. Databus *Databus
  33. Ecode *ecode.Config
  34. }
  35. // DB .
  36. type DB struct {
  37. BilibiliArchive *sql.Config
  38. ArchiveStat *sql.Config
  39. BilibiliTV *sql.Config
  40. }
  41. // Databus .
  42. type Databus struct {
  43. StatView *databus.Config
  44. Archive *databus.Config
  45. UgcTvBinlog *databus.Config
  46. }
  47. // Rank .
  48. type Rank struct {
  49. SwitchAll bool
  50. SwitchIncr bool
  51. RowsLimit int
  52. Ticker time.Duration
  53. BatchSleep time.Duration
  54. BatchStep time.Duration
  55. FilePath string
  56. FileName string
  57. }
  58. func init() {
  59. flag.StringVar(&confPath, "conf", "", "default config path")
  60. }
  61. // Init init conf
  62. func Init() error {
  63. if confPath != "" {
  64. return local()
  65. }
  66. return remote()
  67. }
  68. func local() (err error) {
  69. _, err = toml.DecodeFile(confPath, &Conf)
  70. return
  71. }
  72. func remote() (err error) {
  73. if client, err = conf.New(); err != nil {
  74. return
  75. }
  76. if err = load(); err != nil {
  77. return
  78. }
  79. go func() {
  80. for range client.Event() {
  81. log.Info("config reload")
  82. if load() != nil {
  83. log.Error("config reload error (%v)", err)
  84. }
  85. }
  86. }()
  87. return
  88. }
  89. func load() (err error) {
  90. var (
  91. s string
  92. ok bool
  93. tmpConf *Config
  94. )
  95. if s, ok = client.Toml2(); !ok {
  96. return errors.New("load config center error")
  97. }
  98. if _, err = toml.Decode(s, &tmpConf); err != nil {
  99. return errors.New("could not decode config")
  100. }
  101. *Conf = *tmpConf
  102. return
  103. }