conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. ecode "go-common/library/ecode/tip"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/middleware/verify"
  11. "go-common/library/net/trace"
  12. "github.com/BurntSushi/toml"
  13. )
  14. var (
  15. confPath string
  16. client *conf.Client
  17. // Conf config
  18. Conf = &Config{}
  19. )
  20. // Config .
  21. type Config struct {
  22. Log *log.Config
  23. BM *bm.ServerConfig
  24. Verify *verify.Config
  25. Tracer *trace.Config
  26. Redis *redis.Config
  27. Ecode *ecode.Config
  28. ItemCFJob *JobConfig
  29. Hadoop *HadoopConfig
  30. UserAreaJob *JobConfig
  31. }
  32. // HadoopConfig ...
  33. type HadoopConfig struct {
  34. HadoopDir string
  35. TarUrl string
  36. }
  37. // JobConfig ...
  38. type JobConfig struct {
  39. Schedule string
  40. // 多少个goroutine同时去写redis数据
  41. WorkerNum int
  42. // 如果指定了文件,使用这个文件
  43. // 如果没有指定,自动去下载文件
  44. // 可以是本地文件地址,或者http文件地址
  45. InputFile string
  46. // 在hadoop里面文件的路径,带日期
  47. HadoopFile string
  48. // Hadoop下载到本地的路径,带日期
  49. LocalTmpFile string
  50. }
  51. func init() {
  52. flag.StringVar(&confPath, "conf", "", "default config path")
  53. }
  54. // Init init conf
  55. func Init() error {
  56. if confPath != "" {
  57. return local()
  58. }
  59. return remote()
  60. }
  61. func local() (err error) {
  62. _, err = toml.DecodeFile(confPath, &Conf)
  63. return
  64. }
  65. func remote() (err error) {
  66. if client, err = conf.New(); err != nil {
  67. return
  68. }
  69. if err = load(); err != nil {
  70. return
  71. }
  72. go func() {
  73. for range client.Event() {
  74. log.Info("config reload")
  75. if load() != nil {
  76. log.Error("config reload error (%v)", err)
  77. }
  78. }
  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. }