config.go 885 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package utils
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path/filepath"
  6. "gopkg.in/yaml.v2"
  7. )
  8. // ConfigStructure config YAML representation
  9. type ConfigStructure struct {
  10. Mysql struct {
  11. User string
  12. Password string
  13. Database string
  14. Hostname string
  15. Port int
  16. }
  17. JWT struct {
  18. Secret string
  19. Expiration int64
  20. }
  21. SMTP struct {
  22. Host string
  23. Port string
  24. User string
  25. Password string
  26. }
  27. Server string
  28. }
  29. // MysqlCredentials database credentials
  30. var config ConfigStructure
  31. // SetConfig set credentials from yaml file
  32. func SetConfig(filename string) error {
  33. path, err := filepath.Abs(filename)
  34. if err != nil {
  35. return nil
  36. }
  37. fmt.Println(path)
  38. dat, err := ioutil.ReadFile(path)
  39. if err != nil {
  40. return err
  41. }
  42. yaml.Unmarshal(dat, &config)
  43. return nil
  44. }
  45. // GetConfig get config from YAML file
  46. func GetConfig() ConfigStructure {
  47. return config
  48. }