conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/service/main/identify-game/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/log"
  9. "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. "github.com/BurntSushi/toml"
  15. )
  16. // Conf global variable.
  17. var (
  18. Conf = &Config{}
  19. client *conf.Client
  20. confPath string
  21. )
  22. // Config struct of conf.
  23. type Config struct {
  24. // base
  25. // log
  26. Xlog *log.Config
  27. //Tracer *conf.Tracer
  28. Tracer *trace.Config
  29. // Verify
  30. Verify *verify.Config
  31. // BM
  32. BM *blademaster.ServerConfig
  33. // http client
  34. HTTPClient *blademaster.ClientConfig
  35. // memcache
  36. Memcache *memcache.Config
  37. // url router map
  38. Dispatcher *Dispatcher
  39. // RPCServer rpc server
  40. RPCServer *rpc.ServerConfig
  41. // grpc server
  42. WardenServer *warden.ServerConfig
  43. // passport
  44. Passport *PassportConfig
  45. }
  46. // Dispatcher router map
  47. type Dispatcher struct {
  48. Name string
  49. Oauth map[string]string
  50. RenewToken map[string]string
  51. RegionInfos []*model.RegionInfo
  52. }
  53. // PassportConfig identify config
  54. type PassportConfig struct {
  55. Host map[string]string
  56. }
  57. func local() (err error) {
  58. _, err = toml.DecodeFile(confPath, &Conf)
  59. return
  60. }
  61. func remote() (err error) {
  62. if client, err = conf.New(); err != nil {
  63. return
  64. }
  65. if err = load(); err != nil {
  66. return
  67. }
  68. go func() {
  69. for range client.Event() {
  70. log.Info("config event")
  71. }
  72. }()
  73. return
  74. }
  75. func load() (err error) {
  76. var (
  77. s string
  78. ok bool
  79. tmpConf *Config
  80. )
  81. if s, ok = client.Toml2(); !ok {
  82. return errors.New("load config center error")
  83. }
  84. if _, err = toml.Decode(s, &tmpConf); err != nil {
  85. return errors.New("could not decode config")
  86. }
  87. *Conf = *tmpConf
  88. return
  89. }
  90. func init() {
  91. flag.StringVar(&confPath, "conf", "", "default config path")
  92. }
  93. // Init int config
  94. func Init() error {
  95. if confPath != "" {
  96. return local()
  97. }
  98. return remote()
  99. }