conf.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "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. // httpClinetAsyn
  34. HTTPClientAsyn *bm.ClientConfig
  35. // HTTPShopping
  36. HTTPShopping *bm.ClientConfig
  37. // bm http
  38. BM *HTTPServers
  39. // host
  40. Host *Host
  41. // db
  42. MySQL *MySQL
  43. // rpc client
  44. ArchiveRPC *rpc.ClientConfig
  45. // rpc account
  46. AccountRPC *rpc.ClientConfig
  47. // relationRPC
  48. RelationRPC *rpc.ClientConfig
  49. // rpc client
  50. TagRPC *rpc.ClientConfig
  51. // rpc Article
  52. ArticleRPC *rpc.ClientConfig
  53. // rpc Location
  54. LocationRPC *rpc.ClientConfig
  55. // Infoc2
  56. FeedInfoc2 *infoc.Config
  57. ChannelInfoc2 *infoc.Config
  58. // memcache
  59. Memcache *Memcache
  60. // BroadcastRPC grpc
  61. PGCRPC *warden.ClientConfig
  62. // Square Count
  63. SquareCount int
  64. }
  65. type Host struct {
  66. Bangumi string
  67. Data string
  68. APICo string
  69. Activity string
  70. LiveAPI string
  71. Shopping string
  72. }
  73. type HTTPServers struct {
  74. Outer *bm.ServerConfig
  75. }
  76. type MySQL struct {
  77. Show *sql.Config
  78. Manager *sql.Config
  79. }
  80. type Memcache struct {
  81. Channels *struct {
  82. *memcache.Config
  83. Expire xtime.Duration
  84. }
  85. }
  86. func init() {
  87. flag.StringVar(&confPath, "conf", "", "default config path")
  88. }
  89. // Init init config.
  90. func Init() (err error) {
  91. if confPath != "" {
  92. _, err = toml.DecodeFile(confPath, &Conf)
  93. return
  94. }
  95. err = remote()
  96. return
  97. }
  98. func remote() (err error) {
  99. if client, err = conf.New(); err != nil {
  100. return
  101. }
  102. if err = load(); err != nil {
  103. return
  104. }
  105. client.Watch("app-channel.toml")
  106. go func() {
  107. for range client.Event() {
  108. log.Info("config reload")
  109. if load() != nil {
  110. log.Error("config reload error (%v)", err)
  111. }
  112. }
  113. }()
  114. return
  115. }
  116. func load() (err error) {
  117. var (
  118. s string
  119. ok bool
  120. tmpConf *Config
  121. )
  122. if s, ok = client.Toml2(); !ok {
  123. return errors.New("load config center error")
  124. }
  125. if _, err = toml.Decode(s, &tmpConf); err != nil {
  126. return errors.New("could not decode config")
  127. }
  128. *Conf = *tmpConf
  129. return
  130. }