config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. path, _ := os.Getwd()
  15. filePath := path + "/config/config.yaml"
  16. if f, err := os.Open(filePath); err != nil {
  17. panic(err)
  18. } else {
  19. err := yaml.NewDecoder(f).Decode(&conf)
  20. if err != nil {
  21. panic(err)
  22. }
  23. }
  24. instance = &conf
  25. })
  26. }
  27. //获取配置文档实例
  28. func Instance() *config {
  29. return instance
  30. }
  31. type config struct {
  32. Server server `yaml:"server"`
  33. Database database `yaml:"database"`
  34. Redis redis `yaml:"redis"`
  35. Logger logger `yaml:"logger"`
  36. }
  37. type server struct {
  38. Address string `yaml:address"`
  39. }
  40. type database struct {
  41. Host string `yaml:"host"`
  42. User string `yaml:"user"`
  43. Password string `yaml:"password"`
  44. Port string `yaml:"port"`
  45. Name string `yaml:"name"`
  46. Timezone string `yaml:"timezone"`
  47. }
  48. type redis struct {
  49. Host string `yaml:"host"`
  50. User string `yaml:"user"`
  51. Password string `yaml:"password"`
  52. }
  53. type logger struct {
  54. Path string
  55. Level string
  56. Stdout bool
  57. }