Clean up config handling

This commit is contained in:
Bolke de Bruin
2020-07-19 14:37:31 +02:00
parent 7e62f39579
commit ba679b1266
2 changed files with 32 additions and 15 deletions

View File

@@ -1,5 +1,10 @@
package config
import (
"github.com/spf13/viper"
"log"
)
type Configuration struct {
Server ServerConfig
OpenId OpenIDConfig
@@ -34,3 +39,29 @@ type RDGCapsConfig struct {
DisablePnp bool
DisableDrive bool
}
func init() {
viper.SetDefault("server.certFile", "server.pem")
viper.SetDefault("server.keyFile", "key.pem")
viper.SetDefault("server.port", 443)
}
func Load(configFile string) Configuration {
var conf Configuration
viper.SetConfigName("rdpgw")
viper.SetConfigFile(configFile)
viper.AddConfigPath(".")
viper.SetEnvPrefix("RDPGW")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
log.Printf("No config file found (%s). Using defaults", err)
}
if err := viper.Unmarshal(&conf); err != nil {
log.Fatalf("Cannot unmarshal the config file; %s", err)
}
return conf
}