From 9d7c41c44dfbef5de0da3226c15b62507a9eadda Mon Sep 17 00:00:00 2001 From: Tine Date: Fri, 16 Feb 2024 14:21:13 +0100 Subject: [PATCH] fix(config): read all env variables --- internal/config/config.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 80c5f16..84f4bb8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "log" "os" "strings" @@ -68,6 +67,14 @@ type CronJob struct { Buffer time.Duration `validate:"required"` } +func GetEnvOrDefault(key, def string) string { + value := os.Getenv(key) + if value == "" { + return def + } + return value +} + func NewConfig() *Config { viper.SetConfigName("zdravko") viper.SetConfigType("yaml") @@ -78,24 +85,22 @@ func NewConfig() *Config { viper.AddConfigPath(".") // Set defaults - viper.SetDefault("port", "8000") - viper.SetDefault("rooturl", "http://localhost:8000") - viper.SetDefault("databasepath", "zdravko.db") - viper.SetDefault("oauth2.scopes", "openid profile email") - viper.SetDefault("temporal.databasepath", "temporal.db") - viper.SetDefault("temporal.listenaddress", "0.0.0.0") - viper.SetDefault("temporal.uihost", "127.0.0.1:8223") - viper.SetDefault("temporal.serverhost", "127.0.0.1:7233") - - // Cant figure out the viper env, so lets just do it manually. + viper.SetDefault("port", GetEnvOrDefault("PORT", "8000")) + viper.SetDefault("rooturl", GetEnvOrDefault("ROOT_URL", "http://localhost:8000")) + viper.SetDefault("databasepath", GetEnvOrDefault("DATABASE_PATH", "zdravko.db")) + viper.SetDefault("oauth2.scopes", GetEnvOrDefault("OAUTH2_ENDPOINT_SCOPE", "openid profile email")) viper.SetDefault("sessionsecret", os.Getenv("SESSION_SECRET")) + viper.SetDefault("temporal.databasepath", GetEnvOrDefault("TEMPORAL_DATABASE_PATH", "temporal.db")) + viper.SetDefault("temporal.listenaddress", GetEnvOrDefault("TEMPORAL_LISTEN_ADDRESS", "0.0.0.0")) + viper.SetDefault("temporal.uihost", GetEnvOrDefault("TEMPORAL_UI_HOST", "127.0.0.1:8223")) + viper.SetDefault("temporal.serverhost", GetEnvOrDefault("TEMPORAL_SERVER_HOST", "127.0.0.1:7233")) viper.SetDefault("oauth2.clientid", os.Getenv("OAUTH2_CLIENT_ID")) viper.SetDefault("oauth2.clientsecret", os.Getenv("OAUTH2_CLIENT_SECRET")) viper.SetDefault("oauth2.scope", os.Getenv("OAUTH2_ENDPOINT_SCOPE")) viper.SetDefault("oauth2.endpointtokenurl", os.Getenv("OAUTH2_ENDPOINT_TOKEN_URL")) viper.SetDefault("oauth2.endpointauthurl", os.Getenv("OAUTH2_ENDPOINT_AUTH_URL")) viper.SetDefault("oauth2.endpointuserinfourl", os.Getenv("OAUTH2_ENDPOINT_USER_INFO_URL")) - viper.SetDefault("oauth2.endpointlogouturl", os.Getenv("OAUTH2_ENDPOINT_LOGOUT_URL")) + viper.SetDefault("oauth2.endpointlogouturl", GetEnvOrDefault("OAUTH2_ENDPOINT_LOGOUT_URL", "")) err := viper.ReadInConfig() if err != nil { @@ -123,7 +128,5 @@ func NewConfig() *Config { log.Fatalf("Error validating config, %s", err) } - fmt.Printf("Config: %+v\n", config) - return config }