conf.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/tidb"
  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/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  14. "go-common/library/sync/pipeline"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config .
  25. type Config struct {
  26. Log *log.Config
  27. BM *bm.ServerConfig
  28. GRPC *warden.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *Redis
  32. DataBus *Databus
  33. History *History
  34. Merge *pipeline.Config
  35. TiDB *tidb.Config
  36. }
  37. // Databus .
  38. type Databus struct {
  39. Merge *databus.Config
  40. }
  41. // Redis redis.
  42. type Redis struct {
  43. *redis.Config
  44. Expire xtime.Duration
  45. }
  46. // History .
  47. type History struct {
  48. }
  49. func init() {
  50. flag.StringVar(&confPath, "conf", "", "default config path")
  51. }
  52. // Init init conf
  53. func Init() error {
  54. if confPath != "" {
  55. return local()
  56. }
  57. return remote()
  58. }
  59. func local() (err error) {
  60. _, err = toml.DecodeFile(confPath, &Conf)
  61. return
  62. }
  63. func remote() (err error) {
  64. if client, err = conf.New(); err != nil {
  65. return
  66. }
  67. if err = load(); err != nil {
  68. return
  69. }
  70. return
  71. }
  72. func load() (err error) {
  73. var (
  74. s string
  75. ok bool
  76. tmpConf *Config
  77. )
  78. if s, ok = client.Toml2(); !ok {
  79. return errors.New("load config center error")
  80. }
  81. if _, err = toml.Decode(s, &tmpConf); err != nil {
  82. return errors.New("could not decode config")
  83. }
  84. *Conf = *tmpConf
  85. return
  86. }