conf.go 2.1 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. ecode "go-common/library/ecode/tip"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/middleware/auth"
  12. "go-common/library/net/http/blademaster/middleware/verify"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/rpc/warden"
  15. xtime "go-common/library/time"
  16. "github.com/BurntSushi/toml"
  17. )
  18. // global var
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config config set
  26. type Config struct {
  27. // elk
  28. Log *log.Config
  29. // http
  30. BM *bm.ServerConfig
  31. // Verify
  32. Verify *verify.Config
  33. // Auth
  34. Auth *auth.Config
  35. // memcache
  36. Memcache *Memcache
  37. // MySQL
  38. MySQL *sql.Config
  39. // ecode
  40. Ecode *ecode.Config
  41. // property
  42. Property *Property
  43. // rpc server
  44. RPCServer *rpc.ServerConfig
  45. // grpc server
  46. WardenServer *warden.ServerConfig
  47. }
  48. // Property config for biz logic.
  49. type Property struct {
  50. PointActiveStartDate string
  51. PointActiveEndDate string
  52. ActivityAllowAppID []int64
  53. ConfigLoadTick xtime.Duration
  54. PointGetRule map[string]int64
  55. PointWhiteAppkeys string
  56. }
  57. // Memcache memcache
  58. type Memcache struct {
  59. *memcache.Config
  60. Expire xtime.Duration
  61. }
  62. func init() {
  63. flag.StringVar(&confPath, "conf", "", "default config path")
  64. }
  65. // Init init conf
  66. func Init() error {
  67. if confPath != "" {
  68. return local()
  69. }
  70. return remote()
  71. }
  72. func local() (err error) {
  73. _, err = toml.DecodeFile(confPath, &Conf)
  74. return
  75. }
  76. func remote() (err error) {
  77. if client, err = conf.New(); err != nil {
  78. return
  79. }
  80. if err = load(); err != nil {
  81. return
  82. }
  83. go func() {
  84. for range client.Event() {
  85. log.Info("config reload")
  86. if load() != nil {
  87. log.Error("config reload error (%v)", err)
  88. }
  89. }
  90. }()
  91. return
  92. }
  93. func load() (err error) {
  94. var (
  95. s string
  96. ok bool
  97. tmpConf *Config
  98. )
  99. if s, ok = client.Toml2(); !ok {
  100. return errors.New("load config center error")
  101. }
  102. if _, err = toml.Decode(s, &tmpConf); err != nil {
  103. return errors.New("could not decode config")
  104. }
  105. *Conf = *tmpConf
  106. return
  107. }