conf.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/database/tidb"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus"
  11. "go-common/library/queue/databus/databusutil"
  12. "go-common/library/sync/pipeline"
  13. xtime "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. // Conf global variable.
  18. Conf = &Config{}
  19. client *conf.Client
  20. confPath string
  21. )
  22. // Config .
  23. type Config struct {
  24. Redis *Redis
  25. Tidb *tidb.Config
  26. ItemTidb *tidb.Config
  27. Log *log.Config
  28. Databus *Databus
  29. Thumbup *Thumbup
  30. Memcache *Memcache
  31. StatMerge *StatMerge
  32. Merge *pipeline.Config
  33. LikeDatabusutil *databusutil.Config
  34. ItemLikesDatabusutil *databusutil.Config
  35. UserLikesDatabusutil *databusutil.Config
  36. }
  37. // StatMerge .
  38. type StatMerge struct {
  39. Business string
  40. Target int64
  41. Sources []int64
  42. }
  43. // Redis .
  44. type Redis struct {
  45. *redis.Config
  46. StatsExpire xtime.Duration
  47. UserLikesExpire xtime.Duration
  48. ItemLikesExpire xtime.Duration
  49. }
  50. // Memcache .
  51. type Memcache struct {
  52. *memcache.Config
  53. StatsExpire xtime.Duration
  54. }
  55. // Databus .
  56. type Databus struct {
  57. Stat *databus.Config
  58. Like *databus.Config
  59. ItemLikes *databus.Config
  60. UserLikes *databus.Config
  61. }
  62. // Thumbup .
  63. type Thumbup struct {
  64. }
  65. func init() {
  66. flag.StringVar(&confPath, "conf", "", "default config path")
  67. }
  68. // Init create config instance.
  69. func Init() (err error) {
  70. if confPath != "" {
  71. return local()
  72. }
  73. return remote()
  74. }
  75. func local() (err error) {
  76. _, err = toml.DecodeFile(confPath, &Conf)
  77. return
  78. }
  79. func remote() (err error) {
  80. if client, err = conf.New(); err != nil {
  81. return
  82. }
  83. err = load()
  84. return
  85. }
  86. func load() (err error) {
  87. str, ok := client.Toml2()
  88. if !ok {
  89. return errors.New("load config center error")
  90. }
  91. var tmpConf *Config
  92. if _, err = toml.Decode(str, &tmpConf); err != nil {
  93. return errors.New("could not decode config")
  94. }
  95. *Conf = *tmpConf
  96. return
  97. }