feat: add check for .env file existence before loading environment variables

This commit is contained in:
Ardeman 2025-02-27 22:32:38 +08:00
parent 84239864c1
commit 4ec50cee8a

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"os"
"legalgo-BE-go/config" "legalgo-BE-go/config"
"legalgo-BE-go/database" "legalgo-BE-go/database"
@ -18,8 +19,14 @@ import (
) )
func init() { func init() {
// Check if the .env file exists
if _, err := os.Stat(".env"); err == nil {
// Load environment variables from .env file if it exists
if err := godotenv.Load(); err != nil { if err := godotenv.Load(); err != nil {
log.Fatal("cannot load environment file") log.Println("Error loading .env file, continuing without it")
}
} else {
log.Println(".env file not found, skipping load")
} }
config.InitEnv() config.InitEnv()