conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/app/job/main/block/model"
  6. "go-common/library/cache/memcache"
  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/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. Log *log.Config
  25. Memcache *memcache.Config
  26. DB *sql.Config
  27. BM *bm.ServerConfig
  28. HTTPClient *bm.ClientConfig
  29. Databus *Databus
  30. AccountNotify *databus.Config
  31. Property *Property
  32. // manager log config
  33. ManagerLog *databus.Config
  34. }
  35. // Databus .
  36. type Databus struct {
  37. Credit *databus.Config
  38. }
  39. // Property .
  40. type Property struct {
  41. LimitExpireCheckLimit int
  42. LimitExpireCheckTick xtime.Duration
  43. CreditExpireCheckLimit int
  44. CreditExpireCheckTick xtime.Duration
  45. MSGURL string
  46. MSG *MSG
  47. Flag *struct {
  48. ExpireCheck bool
  49. CreditSub bool
  50. }
  51. }
  52. // MSG .
  53. type MSG struct {
  54. BlockRemove model.MSG
  55. }
  56. func init() {
  57. flag.StringVar(&confPath, "conf", "", "default config path")
  58. }
  59. // Init init conf
  60. func Init() error {
  61. if confPath != "" {
  62. return local()
  63. }
  64. return remote()
  65. }
  66. func local() (err error) {
  67. _, err = toml.DecodeFile(confPath, &Conf)
  68. return
  69. }
  70. func remote() (err error) {
  71. if client, err = conf.New(); err != nil {
  72. return
  73. }
  74. if err = load(); err != nil {
  75. return
  76. }
  77. go func() {
  78. for range client.Event() {
  79. log.Info("config reload")
  80. if load() != nil {
  81. log.Error("config reload error (%v)", err)
  82. }
  83. }
  84. }()
  85. return
  86. }
  87. func load() (err error) {
  88. var (
  89. s string
  90. ok bool
  91. tmpConf *Config
  92. )
  93. if s, ok = client.Toml2(); !ok {
  94. return errors.New("load config center error")
  95. }
  96. if _, err = toml.Decode(s, &tmpConf); err != nil {
  97. return errors.New("could not decode config")
  98. }
  99. *Conf = *tmpConf
  100. return
  101. }