config.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //Environment file for getting variables
  2. //Currently the only thing it does is set the master password
  3. //Should probably have it take over functions from OS such as port and mongodb connection details
  4. //Reads from the config/environments/dev.yaml file by default
  5. //TODO: Add vars for mongo and remove from OS vars in mongoconn
  6. package config
  7. import (
  8. "os"
  9. "fmt"
  10. "log"
  11. "gopkg.in/yaml.v3"
  12. )
  13. //setting dev by default
  14. func getEnv() string {
  15. env := os.Getenv("APP_ENV")
  16. if len(env) == 0 {
  17. return "dev"
  18. }
  19. return env
  20. }
  21. // Config : application config stored as global variable
  22. var Config *EnvironmentConfig
  23. // EnvironmentConfig :
  24. type EnvironmentConfig struct {
  25. Server ServerConfig `yaml:"server"`
  26. MongoConn MongoConnConfig `yaml:"mongoconn"`
  27. }
  28. // ServerConfig :
  29. type ServerConfig struct {
  30. Host string `yaml:"host"`
  31. ApiPort string `yaml:"apiport"`
  32. GrpcPort string `yaml:"grpcport"`
  33. MasterKey string `yaml:"masterkey"`
  34. AllowedOrigin string `yaml:"allowedorigin"`
  35. RestBackend bool `yaml:"restbackend"`
  36. AgentBackend bool `yaml:"agentbackend"`
  37. }
  38. type MongoConnConfig struct {
  39. User string `yaml:"user"`
  40. Pass string `yaml:"pass"`
  41. Host string `yaml:"host"`
  42. Port string `yaml:"port"`
  43. Opts string `yaml:"opts"`
  44. }
  45. //reading in the env file
  46. func readConfig() *EnvironmentConfig {
  47. file := fmt.Sprintf("config/environments/%s.yaml", getEnv())
  48. f, err := os.Open(file)
  49. if err != nil {
  50. log.Fatal(err)
  51. os.Exit(2)
  52. }
  53. defer f.Close()
  54. var cfg EnvironmentConfig
  55. decoder := yaml.NewDecoder(f)
  56. err = decoder.Decode(&cfg)
  57. if err != nil {
  58. log.Fatal(err)
  59. os.Exit(2)
  60. }
  61. return &cfg
  62. }
  63. func init() {
  64. Config = readConfig()
  65. }