config.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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("NETMAKER_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. WG WG `yaml:"wg"`
  28. }
  29. // ServerConfig :
  30. type ServerConfig struct {
  31. APIHost string `yaml:"apihost"`
  32. APIPort string `yaml:"apiport"`
  33. GRPCHost string `yaml:"grpchost"`
  34. GRPCPort string `yaml:"grpcport"`
  35. MasterKey string `yaml:"masterkey"`
  36. AllowedOrigin string `yaml:"allowedorigin"`
  37. RestBackend string `yaml:"restbackend"`
  38. AgentBackend string `yaml:"agentbackend"`
  39. ClientMode string `yaml:"clientmode"`
  40. DNSMode string `yaml:"dnsmode"`
  41. DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
  42. }
  43. type WG struct {
  44. RegisterKeyRequired string `yaml:"keyrequired"`
  45. GRPCWireGuard string `yaml:"grpcwg"`
  46. GRPCWGInterface string `yaml:"grpciface"`
  47. GRPCWGAddress string `yaml:"grpcaddr"`
  48. GRPCWGAddressRange string `yaml:"grpcaddrrange"`
  49. GRPCWGPort string `yaml:"grpcport"`
  50. GRPCWGPubKey string `yaml:"pubkey"`
  51. GRPCWGPrivKey string `yaml:"privkey"`
  52. }
  53. type MongoConnConfig struct {
  54. User string `yaml:"user"`
  55. Pass string `yaml:"pass"`
  56. Host string `yaml:"host"`
  57. Port string `yaml:"port"`
  58. Opts string `yaml:"opts"`
  59. }
  60. //reading in the env file
  61. func readConfig() *EnvironmentConfig {
  62. file := fmt.Sprintf("config/environments/%s.yaml", getEnv())
  63. f, err := os.Open(file)
  64. var cfg EnvironmentConfig
  65. if err != nil {
  66. //log.Fatal(err)
  67. //os.Exit(2)
  68. //log.Println("Unable to open config file at config/environments/" + getEnv())
  69. //log.Println("Will proceed with defaults or enironment variables (no config file).")
  70. return &cfg
  71. }
  72. defer f.Close()
  73. decoder := yaml.NewDecoder(f)
  74. err = decoder.Decode(&cfg)
  75. if err != nil {
  76. log.Fatal(err)
  77. os.Exit(2)
  78. }
  79. return &cfg
  80. }
  81. func init() {
  82. Config = readConfig()
  83. }