fix(contorller): env corrections on where to look for the file when running

This commit is contained in:
2025-09-30 19:54:10 -05:00
parent 18e57127e2
commit b84ecbf30c
26 changed files with 2637 additions and 27 deletions

45
controller/load_env.go Normal file
View 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")
}
}
}