conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/sql"
  8. "go-common/library/log"
  9. bm "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. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. // Conf global variable.
  18. var (
  19. Conf = &Config{}
  20. client *conf.Client
  21. confPath string
  22. )
  23. // Config login conf
  24. type Config struct {
  25. // tracer
  26. Tracer *trace.Config
  27. // xlog
  28. Xlog *log.Config
  29. // Identify
  30. VerifyConfig *verify.Config
  31. // db
  32. Mysql *sql.Config
  33. // mc
  34. Memcache *Memcache
  35. // app
  36. App *bm.App
  37. // BM
  38. BM *bm.ServerConfig
  39. // RPC
  40. RPCServer *rpc.ServerConfig
  41. // grpc
  42. WardenServer *warden.ServerConfig
  43. DC *DC
  44. ServiceConf *ServiceConf
  45. }
  46. // ServiceConf Switch
  47. type ServiceConf struct {
  48. SupportOld bool
  49. Permit map[string]string
  50. }
  51. // DC DC
  52. type DC struct {
  53. Num int
  54. Desc string
  55. }
  56. // Memcache cache
  57. type Memcache struct {
  58. *memcache.Config
  59. Expire xtime.Duration
  60. }
  61. func local() (err error) {
  62. _, err = toml.DecodeFile(confPath, &Conf)
  63. return
  64. }
  65. func remote() (err error) {
  66. if client, err = conf.New(); err != nil {
  67. return
  68. }
  69. if err = load(); err != nil {
  70. return
  71. }
  72. go func() {
  73. for range client.Event() {
  74. log.Info("config reload")
  75. if load() != nil {
  76. log.Error("config reload error (%v)", err)
  77. }
  78. }
  79. }()
  80. return
  81. }
  82. func load() (err error) {
  83. var (
  84. s string
  85. ok bool
  86. tmpConf *Config
  87. )
  88. if s, ok = client.Value("passport-auth-service.toml"); !ok {
  89. return errors.New("load config center error")
  90. }
  91. if _, err = toml.Decode(s, &tmpConf); err != nil {
  92. return errors.New("could not decode config")
  93. }
  94. *Conf = *tmpConf
  95. return
  96. }
  97. func init() {
  98. flag.StringVar(&confPath, "conf", "", "default config path")
  99. }
  100. // Init int config
  101. func Init() error {
  102. if confPath != "" {
  103. return local()
  104. }
  105. return remote()
  106. }