config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. NetclientAutoUpdate string `yaml:"netclientautoupdate"`
  38. MasterKey string `yaml:"masterkey"`
  39. DNSKey string `yaml:"dnskey"`
  40. AllowedOrigin string `yaml:"allowedorigin"`
  41. NodeID string `yaml:"nodeid"`
  42. RestBackend string `yaml:"restbackend"`
  43. MessageQueueBackend string `yaml:"messagequeuebackend"`
  44. DNSMode string `yaml:"dnsmode"`
  45. DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
  46. Version string `yaml:"version"`
  47. SQLConn string `yaml:"sqlconn"`
  48. Platform string `yaml:"platform"`
  49. Database string `yaml:"database"`
  50. Verbosity int32 `yaml:"verbosity"`
  51. AuthProvider string `yaml:"authprovider"`
  52. OIDCIssuer string `yaml:"oidcissuer"`
  53. ClientID string `yaml:"clientid"`
  54. ClientSecret string `yaml:"clientsecret"`
  55. FrontendURL string `yaml:"frontendurl"`
  56. DisplayKeys string `yaml:"displaykeys"`
  57. AzureTenant string `yaml:"azuretenant"`
  58. Telemetry string `yaml:"telemetry"`
  59. HostNetwork string `yaml:"hostnetwork"`
  60. Server string `yaml:"server"`
  61. PublicIPService string `yaml:"publicipservice"`
  62. MQPassword string `yaml:"mqpassword"`
  63. MQUserName string `yaml:"mqusername"`
  64. MetricsExporter string `yaml:"metrics_exporter"`
  65. BasicAuth string `yaml:"basic_auth"`
  66. LicenseValue string `yaml:"license_value"`
  67. NetmakerAccountID string `yaml:"netmaker_account_id"`
  68. IsEE string `yaml:"is_ee"`
  69. StunPort int `yaml:"stun_port"`
  70. StunList string `yaml:"stun_list"`
  71. Proxy string `yaml:"proxy"`
  72. DefaultProxyMode ProxyMode `yaml:"defaultproxymode"`
  73. }
  74. // ProxyMode - default proxy mode for server
  75. type ProxyMode struct {
  76. Set bool
  77. Value bool
  78. }
  79. // SQLConfig - Generic SQL Config
  80. type SQLConfig struct {
  81. Host string `yaml:"host"`
  82. Port int32 `yaml:"port"`
  83. Username string `yaml:"username"`
  84. Password string `yaml:"password"`
  85. DB string `yaml:"db"`
  86. SSLMode string `yaml:"sslmode"`
  87. }
  88. // reading in the env file
  89. func ReadConfig(absolutePath string) (*EnvironmentConfig, error) {
  90. if len(absolutePath) == 0 {
  91. absolutePath = fmt.Sprintf("environments/%s.yaml", getEnv())
  92. }
  93. f, err := os.Open(absolutePath)
  94. var cfg EnvironmentConfig
  95. if err != nil {
  96. return &cfg, err
  97. }
  98. defer f.Close()
  99. decoder := yaml.NewDecoder(f)
  100. if decoder.Decode(&cfg) != nil {
  101. return &cfg, err
  102. }
  103. return &cfg, err
  104. }