loader.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "gopkg.in/yaml.v3"
  7. )
  8. var config = Config{
  9. Port: 8080,
  10. Sslport: 8443,
  11. ServiceURL: "http://127.0.0.1",
  12. SystemID: "schematic-service",
  13. HealthCheck: HealthCheck{
  14. Period: 30,
  15. },
  16. }
  17. // File the config file
  18. var File = "config/service.yaml"
  19. // Get returns loaded config
  20. func Get() Config {
  21. return config
  22. }
  23. // Load loads the config
  24. func Load() error {
  25. _, err := os.Stat(File)
  26. if err != nil {
  27. return err
  28. }
  29. data, err := ioutil.ReadFile(File)
  30. if err != nil {
  31. return fmt.Errorf("can't load config file: %s", err.Error())
  32. }
  33. err = yaml.Unmarshal(data, &config)
  34. if err != nil {
  35. return fmt.Errorf("can't unmarshal config file: %s", err.Error())
  36. }
  37. return readSecret()
  38. }
  39. func readSecret() error {
  40. secretFile := config.SecretFile
  41. if secretFile != "" {
  42. data, err := ioutil.ReadFile(secretFile)
  43. if err != nil {
  44. return fmt.Errorf("can't load secret file: %s", err.Error())
  45. }
  46. var secretConfig Secret = Secret{}
  47. err = yaml.Unmarshal(data, &secretConfig)
  48. if err != nil {
  49. return fmt.Errorf("can't unmarshal secret file: %s", err.Error())
  50. }
  51. mergeSecret(secretConfig)
  52. }
  53. return nil
  54. }
  55. func mergeSecret(secret Secret) {
  56. // config.MongoDB.Username = secret.MongoDB.Username
  57. // config.MongoDB.Password = secret.MongoDB.Password
  58. }