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 (
|
2021-07-15 04:47:05 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
2021-03-26 00:17:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
//setting dev by default
|
|
|
|
func getEnv() string {
|
|
|
|
|
2021-07-15 04:47:05 +08:00
|
|
|
env := os.Getenv("NETMAKER_ENV")
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-15 04:47:05 +08:00
|
|
|
if len(env) == 0 {
|
|
|
|
return "dev"
|
|
|
|
}
|
2021-03-26 00:17:52 +08:00
|
|
|
|
2021-07-15 04:47:05 +08:00
|
|
|
return env
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config : application config stored as global variable
|
|
|
|
var Config *EnvironmentConfig
|
|
|
|
|
|
|
|
// EnvironmentConfig :
|
|
|
|
type EnvironmentConfig struct {
|
2021-07-22 06:55:19 +08:00
|
|
|
Server ServerConfig `yaml:"server"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerConfig :
|
|
|
|
type ServerConfig struct {
|
2021-07-22 06:55:19 +08:00
|
|
|
CoreDNSAddr string `yaml:"corednsaddr"`
|
2021-07-15 04:47:05 +08:00
|
|
|
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"`
|
|
|
|
MasterKey string `yaml:"masterkey"`
|
|
|
|
AllowedOrigin string `yaml:"allowedorigin"`
|
|
|
|
RestBackend string `yaml:"restbackend"`
|
|
|
|
AgentBackend string `yaml:"agentbackend"`
|
|
|
|
ClientMode string `yaml:"clientmode"`
|
|
|
|
DNSMode string `yaml:"dnsmode"`
|
|
|
|
DisableRemoteIPCheck string `yaml:"disableremoteipcheck"`
|
|
|
|
DisableDefaultNet string `yaml:"disabledefaultnet"`
|
|
|
|
GRPCSSL string `yaml:"grpcssl"`
|
2021-08-17 04:30:55 +08:00
|
|
|
Version string `yaml:"version"`
|
|
|
|
DefaultNodeLimit int32 `yaml:"defaultnodelimit"`
|
2021-07-15 04:47:05 +08:00
|
|
|
Verbosity int32 `yaml:"verbosity"`
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//reading in the env file
|
|
|
|
func readConfig() *EnvironmentConfig {
|
2021-07-15 04:47:05 +08:00
|
|
|
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
|
|
|
|
2021-07-15 04:47:05 +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() {
|
2021-07-15 04:47:05 +08:00
|
|
|
Config = readConfig()
|
2021-03-26 00:17:52 +08:00
|
|
|
}
|