conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/conf"
  7. "go-common/library/log"
  8. "go-common/library/naming/livezk"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/rpc/warden"
  12. "go-common/library/net/trace"
  13. "go-common/library/queue/databus"
  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. // log
  25. Xlog *log.Config
  26. //Tracer *conf.Tracer
  27. Tracer *trace.Config
  28. // VerifyConfig
  29. VerifyConfig *verify.Config
  30. // BM
  31. BM *bm.ServerConfig
  32. // http client
  33. HTTPClient *bm.ClientConfig
  34. // memcache
  35. Memcache *memcache.Config
  36. // MemcacheLoginLog
  37. MemcacheLoginLog *memcache.Config
  38. // grpc server
  39. WardenServer *warden.ServerConfig
  40. // live zookeeper
  41. LiveZK *livezk.Zookeeper
  42. // IdentifyConfig
  43. Identify *IdentifyConfig
  44. // DataBus config
  45. DataBus *DataBus
  46. }
  47. // IdentifyConfig identify config
  48. type IdentifyConfig struct {
  49. AuthHost string
  50. // LoginLogConsumerSize goroutine size
  51. LoginLogConsumerSize int
  52. // LoginCacheExpires login check cache expires
  53. LoginCacheExpires int32
  54. // IntranetCIDR
  55. IntranetCIDR []string
  56. }
  57. // DataBus data bus config
  58. type DataBus struct {
  59. UserLog *databus.Config
  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 event")
  75. }
  76. }()
  77. return
  78. }
  79. func load() (err error) {
  80. var (
  81. s string
  82. ok bool
  83. tmpConf *Config
  84. )
  85. if s, ok = client.Toml2(); !ok {
  86. return errors.New("load config center error")
  87. }
  88. if _, err = toml.Decode(s, &tmpConf); err != nil {
  89. return errors.New("could not decode config")
  90. }
  91. *Conf = *tmpConf
  92. return
  93. }
  94. func init() {
  95. flag.StringVar(&confPath, "conf", "", "default config path")
  96. }
  97. // Init int config
  98. func Init() error {
  99. if confPath != "" {
  100. return local()
  101. }
  102. return remote()
  103. }