conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/trace"
  12. "go-common/library/queue/databus"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. client *conf.Client
  18. // Conf config
  19. Conf = &Config{}
  20. )
  21. // Config .
  22. type Config struct {
  23. Log *log.Config
  24. BM *bm.ServerConfig
  25. Verify *verify.Config
  26. Tracer *trace.Config
  27. Ecode *ecode.Config
  28. Riot *RiotConfig
  29. HTTPClient *bm.ClientConfig
  30. Databus *databus.Config
  31. // db
  32. Mysql *sql.Config
  33. UT bool
  34. }
  35. // RiotConfig configs
  36. type RiotConfig struct {
  37. Dict string
  38. StopToken string
  39. LoadPath string
  40. // timeout ms
  41. Timeout int
  42. // interval(second) to flush index
  43. FlushTime int64
  44. // 索引器shards数目
  45. NumShards int
  46. // 持久化文件数目,尽量保证每个文件小于100M
  47. StoreShards int
  48. StoreFolder string
  49. // 持久化存储引擎,可选项bg(badger),leveldb,bolt
  50. StoreEngine string
  51. }
  52. func init() {
  53. flag.StringVar(&confPath, "conf", "", "default config path")
  54. }
  55. // Init init conf
  56. func Init() error {
  57. if confPath != "" {
  58. return local()
  59. }
  60. return remote()
  61. }
  62. func local() (err error) {
  63. _, err = toml.DecodeFile(confPath, &Conf)
  64. return
  65. }
  66. func remote() (err error) {
  67. if client, err = conf.New(); err != nil {
  68. return
  69. }
  70. if err = load(); err != nil {
  71. return
  72. }
  73. go func() {
  74. for range client.Event() {
  75. log.Info("config reload")
  76. if load() != nil {
  77. log.Error("config reload error (%v)", err)
  78. }
  79. }
  80. }()
  81. return
  82. }
  83. func load() (err error) {
  84. var (
  85. s string
  86. ok bool
  87. tmpConf *Config
  88. )
  89. if s, ok = client.Toml2(); !ok {
  90. return errors.New("load config center error")
  91. }
  92. if _, err = toml.Decode(s, &tmpConf); err != nil {
  93. return errors.New("could not decode config")
  94. }
  95. *Conf = *tmpConf
  96. return
  97. }