config.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package lancerroute
  2. import (
  3. "time"
  4. "fmt"
  5. "go-common/app/service/ops/log-agent/conf/configcenter"
  6. "go-common/library/log"
  7. "github.com/BurntSushi/toml"
  8. )
  9. const (
  10. lancerRouteFile = "lancerRoute.toml"
  11. )
  12. type Config struct {
  13. LancerRoute map[string]string `toml:"lancerroute"`
  14. }
  15. func (l *Lancerroute) InitConfig() (err error) {
  16. // read config from config center
  17. if err = l.readConfig(); err != nil {
  18. return err
  19. }
  20. // watch update and reload config
  21. go func() {
  22. currentVersion := configcenter.Version
  23. for {
  24. if currentVersion != configcenter.Version {
  25. log.Info("lancer route config reload")
  26. if err := l.readConfig(); err != nil {
  27. log.Error("lancer route config reload error (%v", err)
  28. }
  29. currentVersion = configcenter.Version
  30. }
  31. time.Sleep(time.Second)
  32. }
  33. }()
  34. return nil
  35. }
  36. func (l *Lancerroute) readConfig() (err error) {
  37. var (
  38. ok bool
  39. value string
  40. tmpLancerRoute Config
  41. )
  42. // sample config
  43. if value, ok = configcenter.Client.Value(lancerRouteFile); !ok {
  44. return fmt.Errorf("failed to get %s", lancerRouteFile)
  45. }
  46. if _, err = toml.Decode(value, &tmpLancerRoute); err != nil {
  47. return err
  48. }
  49. l.c = &tmpLancerRoute
  50. return nil
  51. }