conf.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/auth"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/trace"
  15. "go-common/library/net/rpc/warden"
  16. "go-common/library/net/rpc/liverpc"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. // ConfPath 配置地址
  21. ConfPath string
  22. client *conf.Client
  23. // Conf config
  24. Conf = &Config{}
  25. )
  26. type host struct {
  27. PayCenter string
  28. LiveRpc string
  29. }
  30. type httpClient struct {
  31. PayCenter *bm.ClientConfig
  32. LiveRpc *bm.ClientConfig
  33. }
  34. type gift struct {
  35. RechargeTip *rechargeTip
  36. }
  37. type rechargeTip struct {
  38. SilverTipDays []int
  39. }
  40. // Config .
  41. type Config struct {
  42. Log *log.Config
  43. BM *bm.ServerConfig
  44. Verify *verify.Config
  45. Tracer *trace.Config
  46. Redis *redis.Config
  47. Memcache *memcache.Config
  48. MySQL *sql.Config
  49. Ecode *ecode.Config
  50. ResourceClient *warden.ClientConfig
  51. Auth *auth.Config
  52. Warden *warden.ClientConfig
  53. DM *warden.ClientConfig
  54. Risk *warden.ClientConfig
  55. VerifyConf *warden.ClientConfig
  56. Host host
  57. HTTPClient *httpClient
  58. Gift *gift
  59. LiveRpc map[string]*liverpc.ClientConfig
  60. }
  61. func init() {
  62. flag.StringVar(&ConfPath, "conf", "", "default config path")
  63. }
  64. // Init init conf
  65. func Init() error {
  66. if ConfPath != "" {
  67. return local()
  68. }
  69. return remote()
  70. }
  71. func local() (err error) {
  72. _, err = toml.DecodeFile(ConfPath, &Conf)
  73. return
  74. }
  75. func remote() (err error) {
  76. if client, err = conf.New(); err != nil {
  77. return
  78. }
  79. if err = load(); err != nil {
  80. return
  81. }
  82. go func() {
  83. for range client.Event() {
  84. log.Info("config reload")
  85. if load() != nil {
  86. log.Error("config reload error (%v)", err)
  87. }
  88. }
  89. }()
  90. return
  91. }
  92. func load() (err error) {
  93. var (
  94. s string
  95. ok bool
  96. tmpConf *Config
  97. )
  98. if s, ok = client.Toml2(); !ok {
  99. return errors.New("load config center error")
  100. }
  101. if _, err = toml.Decode(s, &tmpConf); err != nil {
  102. return errors.New("could not decode config")
  103. }
  104. *Conf = *tmpConf
  105. return
  106. }