conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/service/main/passport-game/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/trace"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. var (
  16. confPath string
  17. // Conf conf.
  18. Conf = &Config{}
  19. client *conf.Client
  20. )
  21. // Config config.
  22. type Config struct {
  23. // Proxy if proxy
  24. Proxy bool
  25. // URIs
  26. AccountURI string
  27. PassportURI string
  28. // Xlog log
  29. Xlog *log.Config
  30. // Tracer tracer
  31. Tracer *trace.Config
  32. // DB db
  33. DB *DB
  34. // Memcache memcache
  35. Memcache *Memcache
  36. // HTTPClient http client
  37. HTTPClient *bm.ClientConfig
  38. // HTTP server config
  39. BM *bm.ServerConfig
  40. // Dispatcher dispatcher
  41. Dispatcher *Dispatcher
  42. }
  43. // Dispatcher dispatcher.
  44. type Dispatcher struct {
  45. Name string
  46. Oauth map[string]string
  47. RenewToken map[string]string
  48. RegionInfos []*model.RegionInfo
  49. }
  50. // DB db config.
  51. type DB struct {
  52. Cloud *sql.Config
  53. OtherRegion *sql.Config
  54. }
  55. // Memcache general memcache config.
  56. type Memcache struct {
  57. *memcache.Config
  58. Expire time.Duration
  59. }
  60. func init() {
  61. flag.StringVar(&confPath, "conf", "", "default config path")
  62. }
  63. // Init init config.
  64. func Init() (err error) {
  65. if confPath != "" {
  66. return local()
  67. }
  68. return remote()
  69. }
  70. func local() (err error) {
  71. _, err = toml.DecodeFile(confPath, &Conf)
  72. return
  73. }
  74. func remote() (err error) {
  75. if client, err = conf.New(); err != nil {
  76. return
  77. }
  78. if err = load(); err != nil {
  79. return
  80. }
  81. go func() {
  82. for range client.Event() {
  83. log.Info("config reload")
  84. if load() != nil {
  85. log.Error("config reload error (%v)", err)
  86. }
  87. }
  88. }()
  89. return
  90. }
  91. func load() (err error) {
  92. var (
  93. s string
  94. ok bool
  95. tmpConf *Config
  96. )
  97. if s, ok = client.Toml2(); !ok {
  98. return errors.New("load config center error")
  99. }
  100. if _, err = toml.Decode(s, &tmpConf); err != nil {
  101. return errors.New("could not decode config")
  102. }
  103. *Conf = *tmpConf
  104. return
  105. }