conf.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/conf"
  6. "go-common/library/database/sql"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. "go-common/library/log/infoc"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/rpc"
  12. "go-common/library/net/rpc/warden"
  13. "go-common/library/net/trace"
  14. xtime "go-common/library/time"
  15. "github.com/BurntSushi/toml"
  16. )
  17. var (
  18. confPath string
  19. Conf = &Config{}
  20. client *conf.Client
  21. )
  22. type Config struct {
  23. // Env
  24. Env string
  25. // show XLog
  26. Log *log.Config
  27. // tick time
  28. Tick xtime.Duration
  29. // tracer
  30. Tracer *trace.Config
  31. // httpClinet
  32. HTTPClient *bm.ClientConfig
  33. // bm http
  34. BM *HTTPServers
  35. // db
  36. Ecode *ecode.Config
  37. MySQL *MySQL
  38. // duration
  39. Duration *Duration
  40. // Splash
  41. Splash *Splash
  42. // interestJSONFile
  43. InterestJSONFile string
  44. // StaticJsonFile
  45. StaticJSONFile string
  46. // guide rand
  47. GuideRandom *GuideRandom
  48. // domain
  49. Domain *Domain
  50. ABTest *ABTest
  51. // host
  52. Host *Host
  53. // sideBar limit id
  54. SideBarLimit []int64
  55. // resource
  56. ResourceRPC *rpc.ClientConfig
  57. // infoc2
  58. InterestInfoc *infoc.Config
  59. // BroadcastRPC grpc
  60. BroadcastRPC *warden.ClientConfig
  61. // White
  62. White *White
  63. // 垃圾白名单
  64. ShowTabMids []int64
  65. // location rpc
  66. LocationRPC *rpc.ClientConfig
  67. // show hot all
  68. ShowHotAll bool
  69. // rpc server2
  70. RPCServer *rpc.ServerConfig
  71. }
  72. type HTTPServers struct {
  73. Outer *bm.ServerConfig
  74. }
  75. type Host struct {
  76. Ad string
  77. Data string
  78. VC string
  79. }
  80. type White struct {
  81. List map[string][]string
  82. }
  83. type ABTest struct {
  84. Range int
  85. }
  86. type GuideRandom struct {
  87. Random map[string]int
  88. Buvid map[string]int
  89. Feed uint32
  90. }
  91. type Duration struct {
  92. // splash
  93. Splash string
  94. }
  95. type Splash struct {
  96. Random map[string][]string
  97. }
  98. type MySQL struct {
  99. Show *sql.Config
  100. Resource *sql.Config
  101. }
  102. type Domain struct {
  103. Addr []string
  104. ImageAddr []string
  105. }
  106. func init() {
  107. flag.StringVar(&confPath, "conf", "", "config path")
  108. }
  109. func Init() error {
  110. if confPath != "" {
  111. return local()
  112. }
  113. return remote()
  114. }
  115. func local() (err error) {
  116. _, err = toml.DecodeFile(confPath, &Conf)
  117. return
  118. }
  119. func remote() (err error) {
  120. if client, err = conf.New(); err != nil {
  121. return
  122. }
  123. if err = load(); err != nil {
  124. return
  125. }
  126. client.Watch("app-resource.toml")
  127. go func() {
  128. for range client.Event() {
  129. log.Info("config reload")
  130. if load() != nil {
  131. log.Error("config reload error(%v)", err)
  132. }
  133. }
  134. }()
  135. return
  136. }
  137. func load() (err error) {
  138. var (
  139. s string
  140. ok bool
  141. tmpConf *Config
  142. )
  143. if s, ok = client.Toml2(); !ok {
  144. return errors.New("load config center error")
  145. }
  146. if _, err = toml.Decode(s, &tmpConf); err != nil {
  147. return errors.New("could not decode config")
  148. }
  149. *Conf = *tmpConf
  150. return
  151. }