conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  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. NotifyURL string
  25. // base
  26. // log
  27. Xlog *log.Config
  28. // tracer
  29. Tracer *trace.Config
  30. // app
  31. App *APP
  32. // bm service
  33. BM *bm.ServerConfig
  34. // db
  35. Mysql *sql.Config
  36. // mecache
  37. Memcache *Memcache
  38. PendantRedis *PendantRedis
  39. // http client
  40. HTTPClient *bm.ClientConfig
  41. PageSize int64
  42. Properties *Properties
  43. SuitRPC *rpc.ClientConfig
  44. Databus *Databus
  45. }
  46. // Databus .
  47. type Databus struct {
  48. AccountNotify *databus.Config
  49. VipBinLog *databus.Config
  50. }
  51. // Memcache define memcache conf.
  52. type Memcache struct {
  53. *memcache.Config
  54. }
  55. // PendantRedis pendant redis
  56. type PendantRedis struct {
  57. *redis.Config
  58. }
  59. // APP appkey and sec
  60. type APP struct {
  61. Key string
  62. Secret string
  63. }
  64. // Properties app config.
  65. type Properties struct {
  66. UpInfoURL string
  67. MedalCron string
  68. }
  69. func local() (err error) {
  70. _, err = toml.DecodeFile(confPath, &Conf)
  71. return
  72. }
  73. func remote() (err error) {
  74. if client, err = conf.New(); err != nil {
  75. return
  76. }
  77. if err = load(); err != nil {
  78. return
  79. }
  80. go func() {
  81. for range client.Event() {
  82. log.Info("config reload")
  83. if load() != nil {
  84. log.Error("config reload error (%v)", err)
  85. }
  86. }
  87. }()
  88. return
  89. }
  90. func load() (err error) {
  91. var (
  92. s string
  93. ok bool
  94. tmpConf *Config
  95. )
  96. if s, ok = client.Toml2(); !ok {
  97. return errors.New("load config center error")
  98. }
  99. if _, err = toml.Decode(s, &tmpConf); err != nil {
  100. return errors.New("could not decode config")
  101. }
  102. *Conf = *tmpConf
  103. return
  104. }
  105. func init() {
  106. flag.StringVar(&confPath, "conf", "", "default config path")
  107. }
  108. // Init int config
  109. func Init() error {
  110. if confPath != "" {
  111. return local()
  112. }
  113. return remote()
  114. }