conf.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/trace"
  12. "go-common/library/queue/databus"
  13. "go-common/library/time"
  14. "github.com/BurntSushi/toml"
  15. )
  16. var (
  17. // ConfPath dm-job config file path
  18. ConfPath string
  19. client *conf.Client
  20. // Conf config export var
  21. Conf = &Config{}
  22. )
  23. // Config dm-job config struct
  24. type Config struct {
  25. // base
  26. // log
  27. Xlog *log.Config
  28. // tracer
  29. Tracer *trace.Config
  30. // http
  31. HTTPServer *bm.ServerConfig
  32. // databus
  33. Databus *Databus
  34. // database
  35. DB *DB
  36. // redis
  37. Redis *Redis
  38. // memcache
  39. Memcache *Memcache
  40. }
  41. // Databus databus.
  42. type Databus struct {
  43. DMMetaCsmr *databus.Config
  44. }
  45. // DB bilibili_dm
  46. type DB struct {
  47. DMReader *sql.Config
  48. DMWriter *sql.Config
  49. }
  50. // Redis dm redis
  51. type Redis struct {
  52. *redis.Config
  53. Expire time.Duration
  54. }
  55. // Memcache dm memcache
  56. type Memcache struct {
  57. *memcache.Config
  58. Expire time.Duration
  59. }
  60. func init() {
  61. flag.StringVar(&ConfPath, "conf", "", "config path")
  62. }
  63. //Init int config
  64. func Init() error {
  65. if ConfPath != "" {
  66. return local()
  67. }
  68. return remote()
  69. }
  70. func local() (err error) {
  71. _, err = toml.DecodeFile(ConfPath, &Conf)
  72. return
  73. }
  74. func remote() (err error) {
  75. if client, err = conf.New(); err != nil {
  76. return
  77. }
  78. if err = load(); err != nil {
  79. return
  80. }
  81. go func() {
  82. for range client.Event() {
  83. log.Info("config reload")
  84. if load() != nil {
  85. log.Error("config reload error (%v)", err)
  86. }
  87. }
  88. }()
  89. return
  90. }
  91. func load() (err error) {
  92. var (
  93. s string
  94. ok bool
  95. tmpConf *Config
  96. )
  97. if s, ok = client.Toml2(); !ok {
  98. return errors.New("load config center error")
  99. }
  100. if _, err = toml.Decode(s, &tmpConf); err != nil {
  101. return errors.New("could not decode config")
  102. }
  103. *Conf = *tmpConf
  104. return
  105. }