conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "go-common/library/naming/livezk"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/verify"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. confPath string
  20. // Conf is global config
  21. Conf *Config
  22. )
  23. // Config service config
  24. type Config struct {
  25. // base
  26. // db
  27. DB *DB
  28. // redis
  29. Redis *Redis
  30. // xlog
  31. Log *log.Config
  32. // verify
  33. Verify *verify.Config
  34. // http
  35. BM *BM
  36. Account *warden.ClientConfig
  37. // rpc server2
  38. RPCServer *rpc.ServerConfig
  39. // tracer
  40. Tracer *trace.Config
  41. // file
  42. FilePath string
  43. AnonymFileName string
  44. // filter ip
  45. FilterZone []string
  46. // grpc server
  47. WardenServer *warden.ServerConfig
  48. LiveZK *livezk.Zookeeper
  49. // Host
  50. Host *Host
  51. // AnonymKey
  52. AnonymKey string
  53. // new library
  54. IPv4Name string
  55. IPv6Name string
  56. // httpClinet
  57. HTTPClient *bm.ClientConfig
  58. }
  59. // BM http
  60. type BM struct {
  61. Inner *bm.ServerConfig
  62. Local *bm.ServerConfig
  63. }
  64. // DB define MySQL config
  65. type DB struct {
  66. Zlimit *sql.Config
  67. }
  68. // Redis define Redis config
  69. type Redis struct {
  70. Zlimit *Zlimit
  71. }
  72. // Zlimit struct about zlimit
  73. type Zlimit struct {
  74. *redis.Config
  75. Expire time.Duration
  76. }
  77. // Host url
  78. type Host struct {
  79. Maxmind string
  80. Bvcip string
  81. }
  82. func init() {
  83. flag.StringVar(&confPath, "conf", "", "default config path")
  84. }
  85. // Init init config
  86. func Init() (err error) {
  87. if confPath != "" {
  88. _, err = toml.DecodeFile(confPath, &Conf)
  89. return
  90. }
  91. err = configCenter()
  92. return
  93. }
  94. // configCenter ugc
  95. func configCenter() (err error) {
  96. var (
  97. client *conf.Client
  98. c string
  99. ok bool
  100. )
  101. if client, err = conf.New(); err != nil {
  102. panic(err)
  103. }
  104. if c, ok = client.Toml2(); !ok {
  105. err = errors.New("load config center error")
  106. return
  107. }
  108. _, err = toml.Decode(c, &Conf)
  109. go func() {
  110. for e := range client.Event() {
  111. log.Error("get config from config center error(%v)", e)
  112. }
  113. }()
  114. return
  115. }