config.go 802 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package config
  2. import (
  3. "gopkg.in/yaml.v2"
  4. "os"
  5. "sync"
  6. )
  7. var (
  8. instance *config
  9. once sync.Once
  10. )
  11. func init() {
  12. once.Do(func() {
  13. var conf config
  14. filePath := "./edge/config/config.yaml"
  15. if f, err := os.Open(filePath); err != nil {
  16. panic(err)
  17. } else {
  18. err := yaml.NewDecoder(f).Decode(&conf)
  19. if err != nil {
  20. panic(err)
  21. }
  22. }
  23. instance = &conf
  24. })
  25. }
  26. // 获取配置文档实例
  27. func Instance() *config {
  28. return instance
  29. }
  30. type config struct {
  31. Logger logger `yaml:"logger"`
  32. Mqtt mqtt `yaml:"mqtt"`
  33. }
  34. type logger struct {
  35. Path string `yaml:"path"`
  36. Name string `yaml:"name"`
  37. Level string `yaml:"level"`
  38. }
  39. type mqtt struct {
  40. Server string `yaml:"server"`
  41. Id string `yaml:"id"`
  42. User string `yaml:"user"`
  43. Password string `yaml:"password"`
  44. }