conf.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "path"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  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. "go-common/library/time"
  15. "go-common/library/database/hbase.v2"
  16. "github.com/BurntSushi/toml"
  17. )
  18. var (
  19. // ConfPath local config path
  20. ConfPath string
  21. // Conf config
  22. Conf = &Config{}
  23. client *conf.Client
  24. )
  25. // Config str
  26. type Config struct {
  27. // base
  28. // channal len
  29. ChanSize int64
  30. // log
  31. Log *log.Config
  32. // http
  33. BM *bm.ServerConfig
  34. // identify
  35. AppConf *bm.App
  36. // tracer
  37. Tracer *trace.Config
  38. // tick load pgc
  39. Tick time.Duration
  40. // orm
  41. DB *DB
  42. //redis
  43. Redis *Redis
  44. // http client of search
  45. HTTPClient *HTTPClient
  46. // host
  47. Host *Host
  48. // rpc
  49. ArticleRPC *rpc.ClientConfig
  50. AccountRPC *rpc.ClientConfig
  51. VipRPC *rpc.ClientConfig
  52. AccCliConf *warden.ClientConfig
  53. // threshold
  54. Threshold *Threshold
  55. // hbase
  56. HBase *HBaseConfig
  57. // newbie
  58. Newbie Newbie
  59. }
  60. // DB def db struct
  61. type DB struct {
  62. Allowance *sql.Config
  63. Growup *sql.Config
  64. }
  65. // Redis define redis conf.
  66. type Redis struct {
  67. *redis.Config
  68. Expire time.Duration
  69. }
  70. // HTTPClient http client
  71. type HTTPClient struct {
  72. Read *bm.ClientConfig
  73. }
  74. // Host http host
  75. type Host struct {
  76. AccountURI string
  77. ArchiveURI string
  78. UperURI string
  79. VipURI string
  80. ActivitiesURI string
  81. RelationsURI string
  82. VideoUpURI string
  83. CategoriesURI string
  84. }
  85. // HBaseConfig for new hbase client.
  86. type HBaseConfig struct {
  87. *hbase.Config
  88. WriteTimeout time.Duration
  89. ReadTimeout time.Duration
  90. }
  91. // Threshold up cretive threshold
  92. type Threshold struct {
  93. LimitFanCnt int64
  94. LimitTotalClick int64
  95. LimitArticleView int64
  96. }
  97. // Newbie newbie config
  98. type Newbie struct {
  99. Talents map[string]string
  100. DefaultCover string
  101. DefaultTalent string
  102. RecommendUpCount int
  103. RecommendUpPoolCount int
  104. ActivityCount int
  105. ActivityShotType int32
  106. }
  107. func init() {
  108. flag.StringVar(&ConfPath, "conf", "", "default config path")
  109. }
  110. // Init init conf
  111. func Init() (err error) {
  112. if ConfPath != "" {
  113. return local()
  114. }
  115. return remote()
  116. }
  117. func local() (err error) {
  118. _, err = toml.DecodeFile(ConfPath, &Conf)
  119. if err != nil {
  120. return
  121. }
  122. var templateConfPath = path.Join(path.Dir(ConfPath), "newbie.toml")
  123. _, err = toml.DecodeFile(templateConfPath, &Conf)
  124. return
  125. }
  126. func remote() (err error) {
  127. if client, err = conf.New(); err != nil {
  128. return
  129. }
  130. if err = load(); err != nil {
  131. return
  132. }
  133. go func() {
  134. for range client.Event() {
  135. log.Info("config reload")
  136. if load() != nil {
  137. log.Error("config reload error (%v)", err)
  138. }
  139. }
  140. }()
  141. return
  142. }
  143. func load() (err error) {
  144. var (
  145. s string
  146. ok bool
  147. tmpConf *Config
  148. )
  149. if s, ok = client.Toml2(); !ok {
  150. return errors.New("load config center error")
  151. }
  152. if _, err = toml.Decode(s, &tmpConf); err != nil {
  153. return errors.New("could not decode config")
  154. }
  155. *Conf = *tmpConf
  156. return
  157. }