conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/admin/main/mcn/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/conf"
  8. "go-common/library/database/orm"
  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/antispam"
  13. "go-common/library/net/rpc/warden"
  14. "go-common/library/net/trace"
  15. "go-common/library/time"
  16. "go-common/app/interface/main/mcn/tool/datacenter"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Tracer *trace.Config
  30. Memcache *MemcacheConfig
  31. MCNorm *orm.Config
  32. Ecode *ecode.Config
  33. BFS *BFS
  34. Host *Host
  35. // http client
  36. HTTPClient *bm.ClientConfig
  37. Property *Property
  38. Other *OtherConfig
  39. GRPCClient *GRPCClient
  40. RankCache *GCacheConfig
  41. //upload Antispam
  42. UploadAntispam *antispam.Config
  43. DataClientConf *datacenter.ClientConfig
  44. }
  45. // AfterLoad .
  46. func (s *Config) AfterLoad() {
  47. s.Other.WhiteListMidMap = make(map[int64]struct{}, len(s.Other.WhiteListMid))
  48. for _, v := range s.Other.WhiteListMid {
  49. s.Other.WhiteListMidMap[v] = struct{}{}
  50. }
  51. }
  52. // GRPCClient .
  53. type GRPCClient struct {
  54. Tag *warden.ClientConfig
  55. Account *warden.ClientConfig
  56. Member *warden.ClientConfig
  57. Archive *warden.ClientConfig
  58. }
  59. // MemcacheConfig .
  60. type MemcacheConfig struct {
  61. memcache.Config
  62. McnSignCacheExpire time.Duration
  63. McnDataCacheExpire time.Duration
  64. }
  65. // Property .
  66. type Property struct {
  67. MSG []*model.MSG
  68. }
  69. // BFS bfs config
  70. type BFS struct {
  71. Bucket string
  72. Key string
  73. Secret string
  74. }
  75. // Host host config .
  76. type Host struct {
  77. Bfs string
  78. Msg string
  79. Videoup string
  80. API string
  81. }
  82. //OtherConfig some config.
  83. type OtherConfig struct {
  84. Debug bool
  85. PublicationPriceChangeLimit time.Duration
  86. WhiteListMid []int64 // 超级查看权限
  87. WhiteListMidMap map[int64]struct{}
  88. }
  89. //IsWhiteList check is in white list
  90. func (s *OtherConfig) IsWhiteList(mid int64) bool {
  91. _, ok := s.WhiteListMidMap[mid]
  92. return ok
  93. }
  94. //GCacheConfig gcache
  95. type GCacheConfig struct {
  96. Size int // gcache
  97. ExpireTime time.Duration // key expire time
  98. RecommendPoolExpireTime time.Duration
  99. }
  100. func init() {
  101. flag.StringVar(&confPath, "conf", "", "default config path")
  102. }
  103. // Init init conf
  104. func Init() error {
  105. defer Conf.AfterLoad()
  106. if confPath != "" {
  107. return local()
  108. }
  109. return remote()
  110. }
  111. func local() (err error) {
  112. _, err = toml.DecodeFile(confPath, &Conf)
  113. return
  114. }
  115. func remote() (err error) {
  116. if client, err = conf.New(); err != nil {
  117. return
  118. }
  119. if err = load(); err != nil {
  120. return
  121. }
  122. go func() {
  123. for range client.Event() {
  124. log.Info("config reload")
  125. if load() != nil {
  126. log.Error("config reload error (%v)", err)
  127. }
  128. }
  129. }()
  130. return
  131. }
  132. func load() (err error) {
  133. var (
  134. s string
  135. ok bool
  136. tmpConf *Config
  137. )
  138. if s, ok = client.Toml2(); !ok {
  139. return errors.New("load config center error")
  140. }
  141. if _, err = toml.Decode(s, &tmpConf); err != nil {
  142. return errors.New("could not decode config")
  143. }
  144. *Conf = *tmpConf
  145. return
  146. }