enviroment values

This commit is contained in:
Peder Vatn Austad
2024-12-29 13:53:21 +01:00
parent b00057b2ea
commit 23a29be059

View File

@@ -3,6 +3,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
) )
@@ -31,5 +32,29 @@ func LoadConfig(file string) (Config, error) {
return config, fmt.Errorf("failed to parse config file: %w", err) return config, fmt.Errorf("failed to parse config file: %w", err)
} }
// Override with environment variables if they are set
if host := os.Getenv("DB_HOST"); host != "" {
config.Database.Host = host
}
if port := os.Getenv("DB_PORT"); port != "" {
config.Database.Port = port
}
if user := os.Getenv("DB_USER"); user != "" {
config.Database.User = user
}
if password := os.Getenv("DB_PASSWORD"); password != "" {
config.Database.Password = password
}
if dbname := os.Getenv("DB_NAME"); dbname != "" {
config.Database.DBName = dbname
}
if sslmode := os.Getenv("DB_SSLMODE"); sslmode != "" {
config.Database.SSLMode = sslmode
}
if temp := os.Getenv("TEMP"); temp != "" {
config.Temp = temp
}
log.Println("Temp value:", config.Temp)
return config, nil return config, nil
} }