loader.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "gopkg.in/yaml.v3"
  7. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/logging"
  8. )
  9. var config = Config{
  10. Port: 9080,
  11. Sslport: 9443,
  12. ServiceURL: "http://127.0.0.1",
  13. SystemID: "autorest-srv",
  14. AllowAnonymousBackend: false,
  15. HealthCheck: HealthCheck{
  16. Period: 30,
  17. },
  18. }
  19. // File the config file
  20. var File = "config/service.yaml"
  21. var log logging.ServiceLogger
  22. // Get returns loaded config
  23. func Get() Config {
  24. return config
  25. }
  26. // Load loads the config
  27. func Load() error {
  28. _, err := os.Stat(File)
  29. if err != nil {
  30. return err
  31. }
  32. data, err := ioutil.ReadFile(File)
  33. if err != nil {
  34. return fmt.Errorf("can't load config file: %s", err.Error())
  35. }
  36. err = yaml.Unmarshal(data, &config)
  37. if err != nil {
  38. return fmt.Errorf("can't unmarshal config file: %s", err.Error())
  39. }
  40. readSecret()
  41. value, _ := yaml.Marshal(config)
  42. log.Infof("using configfile %s. \nconfiguration:\n%s\n", File, string(value))
  43. return nil
  44. }
  45. func readSecret() error {
  46. secretFile := config.SecretFile
  47. if secretFile != "" {
  48. data, err := ioutil.ReadFile(secretFile)
  49. if err != nil {
  50. return fmt.Errorf("can't load secret file: %s", err.Error())
  51. }
  52. var secretConfig Secret = Secret{}
  53. err = yaml.Unmarshal(data, &secretConfig)
  54. if err != nil {
  55. return fmt.Errorf("can't unmarshal secret file: %s", err.Error())
  56. }
  57. mergeSecret(secretConfig)
  58. }
  59. return nil
  60. }
  61. func mergeSecret(secret Secret) {
  62. config.MongoDB.Username = secret.MongoDB.Username
  63. config.MongoDB.Password = secret.MongoDB.Password
  64. }