From 3ee15497a5bc080f424feb0008933bb53143c8de Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 6 Jan 2025 14:50:39 +0300 Subject: [PATCH] specify the base directory for loading the .env file --- initializers/load.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/initializers/load.go b/initializers/load.go index 4ea5980..4cbeb0e 100644 --- a/initializers/load.go +++ b/initializers/load.go @@ -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