conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/permit"
  12. "go-common/library/net/rpc"
  13. "go-common/library/net/rpc/warden"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. // global var
  18. var (
  19. confPath string
  20. client *conf.Client
  21. // Conf config
  22. Conf = &Config{}
  23. )
  24. // Config config set
  25. type Config struct {
  26. // base
  27. // elk
  28. Log *log.Config
  29. // http
  30. BM *bm.ServerConfig
  31. // MySQL
  32. MySQL *sql.Config
  33. // ecode
  34. Ecode *ecode.Config
  35. //auth
  36. Auth *permit.Config
  37. Prop *Properties
  38. // rpc client
  39. RPCClient2 *RPC
  40. // memcache
  41. Memcache *Memcache
  42. // BroadcastRPC grpc
  43. PGCRPC *warden.ClientConfig
  44. // http client
  45. HTTPClient *bm.ClientConfig
  46. }
  47. // Properties sysconf.
  48. type Properties struct {
  49. MessageURL string
  50. AllowanceTableCount int64
  51. SalarySleepTime xtime.Duration
  52. SalaryMsgOpen bool
  53. SalaryNormalMsgOpen bool
  54. MsgSysnSize int
  55. }
  56. // Memcache memcache
  57. type Memcache struct {
  58. *memcache.Config
  59. Expire xtime.Duration
  60. }
  61. // RPC config
  62. type RPC struct {
  63. Coupon *rpc.ClientConfig
  64. }
  65. func init() {
  66. flag.StringVar(&confPath, "conf", "", "default config path")
  67. }
  68. // Init init conf
  69. func Init() error {
  70. if confPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. _, err = toml.DecodeFile(confPath, &Conf)
  77. return
  78. }
  79. func remote() (err error) {
  80. if client, err = conf.New(); err != nil {
  81. return
  82. }
  83. if err = load(); err != nil {
  84. return
  85. }
  86. go func() {
  87. for range client.Event() {
  88. log.Info("config reload")
  89. if load() != nil {
  90. log.Error("config reload error (%v)", err)
  91. }
  92. }
  93. }()
  94. return
  95. }
  96. func load() (err error) {
  97. var (
  98. s string
  99. ok bool
  100. tmpConf *Config
  101. )
  102. if s, ok = client.Value("coupon-admin.toml"); !ok {
  103. return errors.New("load config center error")
  104. }
  105. if _, err = toml.Decode(s, &tmpConf); err != nil {
  106. return errors.New("could not decode config")
  107. }
  108. *Conf = *tmpConf
  109. return
  110. }