conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/database/elastic"
  8. "go-common/library/log"
  9. "go-common/library/naming/livezk"
  10. bm "go-common/library/net/http/blademaster"
  11. v "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 common conf
  21. Conf = &Config{}
  22. client *conf.Client
  23. )
  24. //Config config struct
  25. type Config struct {
  26. Host *Host
  27. // log
  28. Log *log.Config
  29. // http
  30. BM *bm.ServerConfig
  31. RPCServer *rpc.ServerConfig
  32. // http client
  33. HTTPClient HTTPClient
  34. // identify
  35. // Identify *identify.Config
  36. // mc
  37. Memcache *Memcache
  38. // tracer
  39. Tracer *trace.Config
  40. // rpc
  41. MemberRPC *rpc.ClientConfig
  42. MemberGRPC *warden.ClientConfig
  43. RelationRPC *rpc.ClientConfig
  44. CoinRPC *rpc.ClientConfig
  45. SuitRPC *rpc.ClientConfig
  46. BlockRPC *rpc.ClientConfig
  47. // warden
  48. WardenServer *warden.ServerConfig
  49. LiveZK *livezk.Zookeeper
  50. // Elastic config
  51. Elastic *elastic.Config
  52. Verify *v.Config
  53. AppkeyFilter *AppkeyFilter
  54. }
  55. // AppkeyFilter is.
  56. type AppkeyFilter struct {
  57. Privacy []string
  58. }
  59. // Host host.
  60. type Host struct {
  61. AccountURI string
  62. VipURI string
  63. PassportURI string
  64. }
  65. // HTTPClient config
  66. type HTTPClient struct {
  67. Read *bm.ClientConfig
  68. Write *bm.ClientConfig
  69. Privacy *bm.ClientConfig
  70. }
  71. // Memcache config
  72. type Memcache struct {
  73. Account *memcache.Config
  74. AccountExpire time.Duration
  75. }
  76. func configCenter() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. panic(err)
  79. }
  80. if err = load(); err != nil {
  81. return
  82. }
  83. go func() {
  84. for range client.Event() {
  85. log.Info("config reload")
  86. if load() != nil {
  87. log.Error("config reload error (%v)", err)
  88. }
  89. }
  90. }()
  91. return
  92. }
  93. func load() (err error) {
  94. var (
  95. s string
  96. ok bool
  97. tmpConf *Config
  98. )
  99. if s, ok = client.Toml2(); !ok {
  100. return errors.New("<account-service.toml> is not exists")
  101. }
  102. if _, err = toml.Decode(s, &tmpConf); err != nil {
  103. return errors.New("could not decode config, maybe <account-service.toml> file err")
  104. }
  105. *Conf = *tmpConf
  106. return
  107. }
  108. func init() {
  109. flag.StringVar(&confPath, "conf", "", "default config path")
  110. }
  111. // Init init config
  112. func Init() (err error) {
  113. if confPath == "" {
  114. return configCenter()
  115. }
  116. _, err = toml.DecodeFile(confPath, &Conf)
  117. return
  118. }