config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package config
  6. import (
  7. "fmt"
  8. "os"
  9. "gopkg.in/yaml.v3"
  10. )
  11. // setting dev by default
  12. func getEnv() string {
  13. env := os.Getenv("NETMAKER_ENV")
  14. if len(env) == 0 {
  15. return "dev"
  16. }
  17. return env
  18. }
  19. // Config : application config stored as global variable
  20. var Config *EnvironmentConfig = &EnvironmentConfig{}
  21. var SetupErr error
  22. // EnvironmentConfig - environment conf struct
  23. type EnvironmentConfig struct {
  24. Server ServerConfig `yaml:"server"`
  25. SQL SQLConfig `yaml:"sql"`
  26. }
  27. // ServerConfig - server conf struct
  28. type ServerConfig struct {
  29. CoreDNSAddr string `yaml:"corednsaddr"`
  30. APIConnString string `yaml:"apiconn"`
  31. APIHost string `yaml:"apihost"`
  32. APIPort string `yaml:"apiport"`
  33. Broker string `yam:"broker"`
  34. ServerBrokerEndpoint string `yaml:"serverbrokerendpoint"`
  35. BrokerType string `yaml:"brokertype"`
  36. EmqxRestEndpoint string `yaml:"emqxrestendpoint"`
  37. MasterKey string `yaml:"masterkey"`
  38. DNSKey string `yaml:"dnskey"`
  39. AllowedOrigin string `yaml:"allowedorigin"`
  40. NodeID string `yaml:"nodeid"`
  41. RestBackend string `yaml:"restbackend"`
  42. MessageQueueBackend string `yaml:"messagequeuebackend"`
  43. DNSMode string `yaml:"dnsmode"`
  44. DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
  45. Version string `yaml:"version"`
  46. SQLConn string `yaml:"sqlconn"`
  47. Platform string `yaml:"platform"`
  48. Database string `yaml:"database"`
  49. Verbosity int32 `yaml:"verbosity"`
  50. AuthProvider string `yaml:"authprovider"`
  51. OIDCIssuer string `yaml:"oidcissuer"`
  52. ClientID string `yaml:"clientid"`
  53. ClientSecret string `yaml:"clientsecret"`
  54. FrontendURL string `yaml:"frontendurl"`
  55. DisplayKeys string `yaml:"displaykeys"`
  56. AzureTenant string `yaml:"azuretenant"`
  57. Telemetry string `yaml:"telemetry"`
  58. HostNetwork string `yaml:"hostnetwork"`
  59. Server string `yaml:"server"`
  60. PublicIPService string `yaml:"publicipservice"`
  61. MQPassword string `yaml:"mqpassword"`
  62. MQUserName string `yaml:"mqusername"`
  63. MetricsExporter string `yaml:"metrics_exporter"`
  64. BasicAuth string `yaml:"basic_auth"`
  65. LicenseValue string `yaml:"license_value"`
  66. NetmakerAccountID string `yaml:"netmaker_account_id"`
  67. IsEE string `yaml:"is_ee"`
  68. StunPort int `yaml:"stun_port"`
  69. StunList string `yaml:"stun_list"`
  70. Proxy string `yaml:"proxy"`
  71. }
  72. // SQLConfig - Generic SQL Config
  73. type SQLConfig struct {
  74. Host string `yaml:"host"`
  75. Port int32 `yaml:"port"`
  76. Username string `yaml:"username"`
  77. Password string `yaml:"password"`
  78. DB string `yaml:"db"`
  79. SSLMode string `yaml:"sslmode"`
  80. }
  81. // reading in the env file
  82. func ReadConfig(absolutePath string) (*EnvironmentConfig, error) {
  83. if len(absolutePath) == 0 {
  84. absolutePath = fmt.Sprintf("environments/%s.yaml", getEnv())
  85. }
  86. f, err := os.Open(absolutePath)
  87. var cfg EnvironmentConfig
  88. if err != nil {
  89. return &cfg, err
  90. }
  91. defer f.Close()
  92. decoder := yaml.NewDecoder(f)
  93. if decoder.Decode(&cfg) != nil {
  94. return &cfg, err
  95. }
  96. return &cfg, err
  97. }