fix(contorller): env corrections on where to look for the file when running
This commit is contained in:
45
controller/load_env.go
Normal file
45
controller/load_env.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func loadEnv() {
|
||||
exePath, _ := os.Executable()
|
||||
exeDir := filepath.Dir(exePath)
|
||||
|
||||
// Normalize both to lowercase absolute paths for Windows safety
|
||||
exePathLower := strings.ToLower(exePath)
|
||||
tempDirLower := strings.ToLower(filepath.Clean(os.TempDir()))
|
||||
|
||||
// Heuristic: if exe lives *inside* the system temp dir → assume go run
|
||||
if strings.HasPrefix(exePathLower, tempDirLower) {
|
||||
fmt.Println("Detected go run – loading ../.env")
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
fmt.Println("ERROR loading .env:", err)
|
||||
} else {
|
||||
fmt.Println(".env successfully loaded")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise → normal compiled exe
|
||||
fmt.Println("Detected compiled exe – loading exeDir/.env")
|
||||
if err := godotenv.Load(filepath.Join(exeDir, ".env")); err != nil {
|
||||
fmt.Println("Didn't find exeDir/.env – trying ../.env as fallback")
|
||||
err := godotenv.Load("../.env")
|
||||
if err != nil {
|
||||
fmt.Println("ERROR loading .env:", err)
|
||||
} else {
|
||||
fmt.Println(".env successfully loaded")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user