conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  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/http/blademaster/middleware/verify"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. xtime "go-common/library/time"
  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. Verify *verify.Config
  27. Tracer *trace.Config
  28. Redis *redis.Config
  29. MallMySQL *sql.Config
  30. TicketMySQL *sql.Config
  31. MallUgcMySQL *sql.Config
  32. PgcSub *databus.Config
  33. ElasticSearch *ElasticSearch
  34. Env string
  35. SourcePath string
  36. Bfs *Bfs
  37. }
  38. func init() {
  39. flag.StringVar(&confPath, "conf", "", "default config path")
  40. }
  41. // Init init conf
  42. func Init() error {
  43. if confPath != "" {
  44. return local()
  45. }
  46. return remote()
  47. }
  48. func local() (err error) {
  49. _, err = toml.DecodeFile(confPath, &Conf)
  50. return
  51. }
  52. func remote() (err error) {
  53. if client, err = conf.New(); err != nil {
  54. return
  55. }
  56. if err = load(); err != nil {
  57. return
  58. }
  59. go func() {
  60. for range client.Event() {
  61. log.Info("config reload")
  62. if load() != nil {
  63. log.Error("config reload error (%v)", err)
  64. }
  65. }
  66. }()
  67. return
  68. }
  69. func load() (err error) {
  70. var (
  71. s string
  72. ok bool
  73. tmpConf *Config
  74. )
  75. if s, ok = client.Toml2(); !ok {
  76. return errors.New("load config center error")
  77. }
  78. if _, err = toml.Decode(s, &tmpConf); err != nil {
  79. return errors.New("could not decode config")
  80. }
  81. *Conf = *tmpConf
  82. return
  83. }
  84. // ElasticSearch config
  85. type ElasticSearch struct {
  86. Addr []string
  87. Check xtime.Duration
  88. Timeout string
  89. Season EsIndex
  90. }
  91. // EsIndex config
  92. type EsIndex struct {
  93. Index string
  94. Type string
  95. Alias string
  96. Mapping string
  97. }
  98. // Bfs config
  99. type Bfs struct {
  100. Timeout xtime.Duration
  101. MaxFileSize int
  102. Bucket string
  103. Addr string
  104. Key string
  105. Secret string
  106. }