netmaker/config/config.go

110 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"
"log"
"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")
2021-03-26 00:17:52 +08:00
if len(env) == 0 {
return "dev"
}
2021-03-26 00:17:52 +08:00
return env
2021-03-26 00:17:52 +08:00
}
// Config : application config stored as global variable
var Config *EnvironmentConfig
// 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"`
GRPCConnString string `yaml:"grpcconn"`
GRPCHost string `yaml:"grpchost"`
GRPCPort string `yaml:"grpcport"`
GRPCSecure string `yaml:"grpcsecure"`
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"`
SplitDNS string `yaml:"splitdns"`
DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
DisableDefaultNet string `yaml:"disabledefaultnet"`
GRPCSSL string `yaml:"grpcssl"`
Version string `yaml:"version"`
SQLConn string `yaml:"sqlconn"`
Platform string `yaml:"platform"`
Database string `yaml:"database"`
CheckinInterval string `yaml:"checkininterval"`
2021-10-21 02:17:31 +08:00
DefaultNodeLimit int32 `yaml:"defaultnodelimit"`
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"`
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
2021-03-26 00:17:52 +08:00
func readConfig() *EnvironmentConfig {
file := fmt.Sprintf("config/environments/%s.yaml", getEnv())
f, err := os.Open(file)
var cfg EnvironmentConfig
if err != nil {
return &cfg
}
defer f.Close()
2021-03-26 00:17:52 +08:00
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
log.Fatal(err)
os.Exit(2)
}
return &cfg
2021-03-26 00:17:52 +08:00
}
func init() {
Config = readConfig()
2021-03-26 00:17:52 +08:00
}