1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //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
- //TODO: Add vars for mongo and remove from OS vars in mongoconn
- package config
- import (
- "os"
- "fmt"
- "log"
- "gopkg.in/yaml.v3"
- )
- //setting dev by default
- func getEnv() string {
- env := os.Getenv("NETMAKER_ENV")
- if len(env) == 0 {
- return "dev"
- }
- return env
- }
- // Config : application config stored as global variable
- var Config *EnvironmentConfig
- // EnvironmentConfig :
- type EnvironmentConfig struct {
- Server ServerConfig `yaml:"server"`
- MongoConn MongoConnConfig `yaml:"mongoconn"`
- WG WG `yaml:"wg"`
- }
- // ServerConfig :
- type ServerConfig struct {
- APIHost string `yaml:"apihost"`
- APIPort string `yaml:"apiport"`
- GRPCHost string `yaml:"grpchost"`
- GRPCPort string `yaml:"grpcport"`
- 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"`
- }
- type WG struct {
- RegisterKeyRequired string `yaml:"keyrequired"`
- GRPCWireGuard string `yaml:"grpcwg"`
- GRPCWGInterface string `yaml:"grpciface"`
- GRPCWGAddress string `yaml:"grpcaddr"`
- GRPCWGAddressRange string `yaml:"grpcaddrrange"`
- GRPCWGPort string `yaml:"grpcport"`
- GRPCWGPubKey string `yaml:"pubkey"`
- GRPCWGPrivKey string `yaml:"privkey"`
- }
- type MongoConnConfig struct {
- User string `yaml:"user"`
- Pass string `yaml:"pass"`
- Host string `yaml:"host"`
- Port string `yaml:"port"`
- Opts string `yaml:"opts"`
- }
- //reading in the env file
- func readConfig() *EnvironmentConfig {
- file := fmt.Sprintf("config/environments/%s.yaml", getEnv())
- f, err := os.Open(file)
- var cfg EnvironmentConfig
- if err != nil {
- //log.Fatal(err)
- //os.Exit(2)
- //log.Println("Unable to open config file at config/environments/" + getEnv())
- //log.Println("Will proceed with defaults or enironment variables (no config file).")
- return &cfg
- }
- defer f.Close()
- decoder := yaml.NewDecoder(f)
- err = decoder.Decode(&cfg)
- if err != nil {
- log.Fatal(err)
- os.Exit(2)
- }
- return &cfg
- }
- func init() {
- Config = readConfig()
- }
|