conf.go 2.3 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/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/rpc"
  11. "go-common/library/net/trace"
  12. "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // Conf global variable.
  16. var (
  17. Conf = &Config{}
  18. client *conf.Client
  19. confPath string
  20. )
  21. // Config struct of conf.
  22. type Config struct {
  23. // log
  24. Xlog *log.Config
  25. // BM
  26. BM *bm.ServerConfig
  27. // rpc server
  28. RPCServer *rpc.ServerConfig
  29. // redis
  30. MultiRedis *MultiRedis
  31. // memcache
  32. Memcache *Memcache
  33. // tracer
  34. Tracer *trace.Config
  35. // rpc client
  36. ArchiveRPC *rpc.ClientConfig
  37. AccountRPC *rpc.ClientConfig
  38. ArticleRPC *rpc.ClientConfig
  39. // httpClient
  40. HTTPClient *bm.ClientConfig
  41. // feed
  42. Feed *Feed
  43. }
  44. // Feed .
  45. type Feed struct {
  46. AppLength int
  47. WebLength int
  48. ArchiveFeedLength int
  49. ArticleFeedLength int
  50. ArchiveFeedExpire time.Duration
  51. BangumiFeedExpire time.Duration
  52. AppPullInterval time.Duration
  53. WebPullInterval time.Duration
  54. ArtPullInterval time.Duration
  55. BulkSize int
  56. MinUpCnt int
  57. MaxTotalCnt int
  58. }
  59. // MultiRedis .
  60. type MultiRedis struct {
  61. MaxArcsNum int
  62. TTLUpper time.Duration
  63. ExpireUpper time.Duration
  64. ExpireFeed time.Duration
  65. Local *redis.Config
  66. Cache *redis.Config
  67. }
  68. // Memcache .
  69. type Memcache struct {
  70. *memcache.Config
  71. Expire time.Duration
  72. BangumiExpire time.Duration
  73. }
  74. func init() {
  75. flag.StringVar(&confPath, "conf", "", "default config path")
  76. }
  77. // Init init conf
  78. func Init() error {
  79. if confPath != "" {
  80. return local()
  81. }
  82. return remote()
  83. }
  84. func local() (err error) {
  85. _, err = toml.DecodeFile(confPath, &Conf)
  86. return
  87. }
  88. func remote() (err error) {
  89. if client, err = conf.New(); err != nil {
  90. return
  91. }
  92. if err = load(); err != nil {
  93. return
  94. }
  95. go func() {
  96. for range client.Event() {
  97. log.Info("config reload")
  98. if load() != nil {
  99. log.Error("config reload error (%v)", err)
  100. }
  101. }
  102. }()
  103. return
  104. }
  105. func load() (err error) {
  106. var (
  107. s string
  108. ok bool
  109. tmpConf *Config
  110. )
  111. if s, ok = client.Toml2(); !ok {
  112. return errors.New("load config center error")
  113. }
  114. if _, err = toml.Decode(s, &tmpConf); err != nil {
  115. return errors.New("could not decode config")
  116. }
  117. *Conf = *tmpConf
  118. return
  119. }