conf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/orm"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/permit"
  13. "go-common/library/net/rpc"
  14. "go-common/library/net/trace"
  15. "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. // base
  28. // elk
  29. Log *log.Config
  30. // mc
  31. Memcache *Memcache
  32. // auth
  33. Auth *permit.Config
  34. // MultiHTTP
  35. BM *bm.ServerConfig
  36. // http client
  37. HTTPClient *bm.ClientConfig
  38. // tracer
  39. Tracer *trace.Config
  40. // MySQL
  41. MySQL *sql.Config
  42. ORM *ORM
  43. // VipRPC
  44. VipRPC *rpc.ClientConfig
  45. // business config
  46. Property *Property
  47. // ecode
  48. Ecode *ecode.Config
  49. // pay conf
  50. PayConf *PayConf
  51. // bfs
  52. Bfs *Bfs
  53. }
  54. // Bfs reprensents the bfs config
  55. type Bfs struct {
  56. Key string
  57. Secret string
  58. Host string
  59. Timeout int
  60. MaxFileSize int
  61. }
  62. // ORM .
  63. type ORM struct {
  64. Vip *orm.Config
  65. }
  66. // Memcache .
  67. type Memcache struct {
  68. *memcache.Config
  69. Expire time.Duration
  70. }
  71. func init() {
  72. flag.StringVar(&confPath, "conf", "", "default config path")
  73. }
  74. // Property .
  75. type Property struct {
  76. MsgURI string
  77. PayURI string
  78. AnnualVipBcoinDay int
  79. AnnualVipBcoinCouponMoney int
  80. AnnualVipBcoinCouponActivityID int
  81. WelfareBgHost string
  82. }
  83. //PayConf pay config
  84. type PayConf struct {
  85. BaseURL string
  86. CustomerID int64
  87. Token string
  88. OrderNotifyURL string
  89. SignNotifyURL string
  90. RefundURL string
  91. PlanID int32
  92. ProductID string
  93. Version string
  94. ReturnURL string
  95. }
  96. // Init init conf
  97. func Init() error {
  98. if confPath != "" {
  99. return local()
  100. }
  101. return remote()
  102. }
  103. func local() (err error) {
  104. _, err = toml.DecodeFile(confPath, &Conf)
  105. return
  106. }
  107. func remote() (err error) {
  108. if client, err = conf.New(); err != nil {
  109. return
  110. }
  111. if err = load(); err != nil {
  112. return
  113. }
  114. go func() {
  115. for range client.Event() {
  116. log.Info("config reload")
  117. if load() != nil {
  118. log.Error("config reload error (%v)", err)
  119. }
  120. }
  121. }()
  122. return
  123. }
  124. func load() (err error) {
  125. var (
  126. s string
  127. ok bool
  128. tmpConf *Config
  129. )
  130. if s, ok = client.Toml2(); !ok {
  131. return errors.New("load config center error")
  132. }
  133. if _, err = toml.Decode(s, &tmpConf); err != nil {
  134. return errors.New("could not decode config")
  135. }
  136. *Conf = *tmpConf
  137. return
  138. }