conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/netutil"
  11. "go-common/library/queue/databus"
  12. xtime "go-common/library/time"
  13. "github.com/BurntSushi/toml"
  14. )
  15. // global var
  16. var (
  17. confPath string
  18. client *conf.Client
  19. // Conf config
  20. Conf = &Config{}
  21. )
  22. // Config config set
  23. type Config struct {
  24. // base
  25. // elk
  26. Log *log.Config
  27. // http
  28. BM *bm.ServerConfig
  29. // memcache
  30. Memcache *memcache.Config
  31. // MySQL
  32. MySQL *sql.Config
  33. // Databus
  34. DataBus *DataSource
  35. // Properties
  36. Properties *Properties
  37. // Backoff retries config
  38. Backoff *netutil.BackoffConfig
  39. // http client
  40. HTTPClient *bm.ClientConfig
  41. NewYearConf *NewYearConf
  42. }
  43. // NewYearConf .
  44. type NewYearConf struct {
  45. ActID int64
  46. }
  47. // DataSource databus source
  48. type DataSource struct {
  49. CouponBinlog *databus.Config
  50. }
  51. // Properties def.
  52. type Properties struct {
  53. MaxRetries int
  54. BangumiNotifyURL string
  55. CheckInUseCouponCron string
  56. CheckInUseCouponCartoonCron string
  57. NotifyTimeInterval xtime.Duration
  58. }
  59. func init() {
  60. flag.StringVar(&confPath, "conf", "", "default config path")
  61. }
  62. // Init init conf
  63. func Init() error {
  64. if confPath != "" {
  65. return local()
  66. }
  67. return remote()
  68. }
  69. func local() (err error) {
  70. _, err = toml.DecodeFile(confPath, &Conf)
  71. return
  72. }
  73. func remote() (err error) {
  74. if client, err = conf.New(); err != nil {
  75. return
  76. }
  77. if err = load(); err != nil {
  78. return
  79. }
  80. return
  81. }
  82. func load() (err error) {
  83. var (
  84. s string
  85. ok bool
  86. tmpConf *Config
  87. )
  88. if s, ok = client.Toml2(); !ok {
  89. return errors.New("load config center error")
  90. }
  91. if _, err = toml.Decode(s, &tmpConf); err != nil {
  92. return errors.New("could not decode config")
  93. }
  94. *Conf = *tmpConf
  95. return
  96. }