specify the base directory for loading the .env file

This commit is contained in:
Alfred Kamanda 2025-01-06 14:50:39 +03:00
parent 4836162f40
commit 3ee15497a5
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703

View File

@ -3,24 +3,26 @@ package initializers
import (
"log"
"os"
"path"
"strconv"
"github.com/joho/godotenv"
)
func LoadEnvVariables() {
err := godotenv.Load()
func LoadEnvVariables(baseDir string) {
envDir := path.Join(baseDir, ".env")
err := godotenv.Load(envDir)
if err != nil {
log.Fatal("Error loading .env file")
log.Fatal("Error loading .env file", err)
}
}
// Helper to get environment variables with a default fallback
func GetEnv(key, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
return defaultVal
}
// Helper to safely convert environment variables to uint