fix(config): read all env variables

This commit is contained in:
Tine 2024-02-16 14:21:13 +01:00
parent 185fd4923f
commit 9d7c41c44d
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw

View file

@ -1,7 +1,6 @@
package config package config
import ( import (
"fmt"
"log" "log"
"os" "os"
"strings" "strings"
@ -68,6 +67,14 @@ type CronJob struct {
Buffer time.Duration `validate:"required"` Buffer time.Duration `validate:"required"`
} }
func GetEnvOrDefault(key, def string) string {
value := os.Getenv(key)
if value == "" {
return def
}
return value
}
func NewConfig() *Config { func NewConfig() *Config {
viper.SetConfigName("zdravko") viper.SetConfigName("zdravko")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
@ -78,24 +85,22 @@ func NewConfig() *Config {
viper.AddConfigPath(".") viper.AddConfigPath(".")
// Set defaults // Set defaults
viper.SetDefault("port", "8000") viper.SetDefault("port", GetEnvOrDefault("PORT", "8000"))
viper.SetDefault("rooturl", "http://localhost:8000") viper.SetDefault("rooturl", GetEnvOrDefault("ROOT_URL", "http://localhost:8000"))
viper.SetDefault("databasepath", "zdravko.db") viper.SetDefault("databasepath", GetEnvOrDefault("DATABASE_PATH", "zdravko.db"))
viper.SetDefault("oauth2.scopes", "openid profile email") viper.SetDefault("oauth2.scopes", GetEnvOrDefault("OAUTH2_ENDPOINT_SCOPE", "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("sessionsecret", os.Getenv("SESSION_SECRET")) 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.clientid", os.Getenv("OAUTH2_CLIENT_ID"))
viper.SetDefault("oauth2.clientsecret", os.Getenv("OAUTH2_CLIENT_SECRET")) viper.SetDefault("oauth2.clientsecret", os.Getenv("OAUTH2_CLIENT_SECRET"))
viper.SetDefault("oauth2.scope", os.Getenv("OAUTH2_ENDPOINT_SCOPE")) viper.SetDefault("oauth2.scope", os.Getenv("OAUTH2_ENDPOINT_SCOPE"))
viper.SetDefault("oauth2.endpointtokenurl", os.Getenv("OAUTH2_ENDPOINT_TOKEN_URL")) viper.SetDefault("oauth2.endpointtokenurl", os.Getenv("OAUTH2_ENDPOINT_TOKEN_URL"))
viper.SetDefault("oauth2.endpointauthurl", os.Getenv("OAUTH2_ENDPOINT_AUTH_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.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() err := viper.ReadInConfig()
if err != nil { if err != nil {
@ -123,7 +128,5 @@ func NewConfig() *Config {
log.Fatalf("Error validating config, %s", err) log.Fatalf("Error validating config, %s", err)
} }
fmt.Printf("Config: %+v\n", config)
return config return config
} }