netmaker/config/config.go

100 lines
3.2 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
//Environment file for getting variables
//Currently the only thing it does is set the master password
//Should probably have it take over functions from OS such as port and mongodb connection details
//Reads from the config/environments/dev.yaml file by default
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
2021-03-26 00:17:52 +08:00
)
// setting dev by default
2021-03-26 00:17:52 +08:00
func getEnv() string {
env := os.Getenv("NETMAKER_ENV")
if len(env) == 0 {
return "dev"
}
return env
2021-03-26 00:17:52 +08:00
}
// Config : application config stored as global variable
var Config *EnvironmentConfig = &EnvironmentConfig{}
var SetupErr error
2021-03-26 00:17:52 +08:00
// EnvironmentConfig - environment conf struct
2021-03-26 00:17:52 +08:00
type EnvironmentConfig struct {
2021-07-22 06:55:19 +08:00
Server ServerConfig `yaml:"server"`
2021-10-21 02:17:31 +08:00
SQL SQLConfig `yaml:"sql"`
2021-03-26 00:17:52 +08:00
}
// ServerConfig - server conf struct
2021-03-26 00:17:52 +08:00
type ServerConfig struct {
2021-10-21 02:17:31 +08:00
CoreDNSAddr string `yaml:"corednsaddr"`
APIConnString string `yaml:"apiconn"`
APIHost string `yaml:"apihost"`
APIPort string `yaml:"apiport"`
2022-01-13 05:23:34 +08:00
MQHOST string `yaml:"mqhost"`
2021-10-21 02:17:31 +08:00
MasterKey string `yaml:"masterkey"`
2021-11-16 00:42:52 +08:00
DNSKey string `yaml:"dnskey"`
2021-10-21 02:17:31 +08:00
AllowedOrigin string `yaml:"allowedorigin"`
NodeID string `yaml:"nodeid"`
RestBackend string `yaml:"restbackend"`
AgentBackend string `yaml:"agentbackend"`
2022-01-13 05:23:34 +08:00
MessageQueueBackend string `yaml:"messagequeuebackend"`
2021-10-21 02:17:31 +08:00
ClientMode string `yaml:"clientmode"`
DNSMode string `yaml:"dnsmode"`
DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
Version string `yaml:"version"`
SQLConn string `yaml:"sqlconn"`
Platform string `yaml:"platform"`
Database string `yaml:"database"`
2021-10-21 02:17:31 +08:00
DefaultNodeLimit int32 `yaml:"defaultnodelimit"`
2022-04-22 05:48:36 +08:00
Verbosity int32 `yaml:"verbosity"`
ServerCheckinInterval int64 `yaml:"servercheckininterval"`
2021-10-21 02:17:31 +08:00
AuthProvider string `yaml:"authprovider"`
ClientID string `yaml:"clientid"`
ClientSecret string `yaml:"clientsecret"`
2021-10-22 03:28:58 +08:00
FrontendURL string `yaml:"frontendurl"`
2021-11-15 05:50:20 +08:00
DisplayKeys string `yaml:"displaykeys"`
AzureTenant string `yaml:"azuretenant"`
2022-01-06 02:13:03 +08:00
RCE string `yaml:"rce"`
2022-01-21 06:50:42 +08:00
Telemetry string `yaml:"telemetry"`
ManageIPTables string `yaml:"manageiptables"`
PortForwardServices string `yaml:"portforwardservices"`
2022-01-26 13:46:12 +08:00
HostNetwork string `yaml:"hostnetwork"`
2022-02-19 04:18:50 +08:00
MQPort string `yaml:"mqport"`
2022-04-12 22:43:02 +08:00
Server string `yaml:"server"`
2021-03-26 00:17:52 +08:00
}
// SQLConfig - Generic SQL Config
2021-10-09 01:11:31 +08:00
type SQLConfig struct {
2021-10-21 02:17:31 +08:00
Host string `yaml:"host"`
Port int32 `yaml:"port"`
2021-10-09 01:11:31 +08:00
Username string `yaml:"username"`
Password string `yaml:"password"`
2021-10-21 02:17:31 +08:00
DB string `yaml:"db"`
SSLMode string `yaml:"sslmode"`
2021-10-09 01:11:31 +08:00
}
// reading in the env file
func ReadConfig(absolutePath string) (*EnvironmentConfig, error) {
if len(absolutePath) == 0 {
absolutePath = fmt.Sprintf("environments/%s.yaml", getEnv())
}
f, err := os.Open(absolutePath)
var cfg EnvironmentConfig
if err != nil {
return &cfg, err
}
defer f.Close()
2021-03-26 00:17:52 +08:00
decoder := yaml.NewDecoder(f)
if decoder.Decode(&cfg) != nil {
return &cfg, err
}
return &cfg, err
2021-03-26 00:17:52 +08:00
}