2024-10-15 22:41:16 +02:00
|
|
|
package initializers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2025-01-06 12:50:39 +01:00
|
|
|
"path"
|
2024-10-15 22:41:16 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
2025-01-06 12:50:39 +01:00
|
|
|
func LoadEnvVariables(baseDir string) {
|
|
|
|
envDir := path.Join(baseDir, ".env")
|
|
|
|
err := godotenv.Load(envDir)
|
2024-10-15 22:41:16 +02:00
|
|
|
if err != nil {
|
2025-01-06 12:50:39 +01:00
|
|
|
log.Fatal("Error loading .env file", err)
|
2024-10-15 22:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to get environment variables with a default fallback
|
|
|
|
func GetEnv(key, defaultVal string) string {
|
2025-01-06 12:50:39 +01:00
|
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
|
|
return value
|
2024-10-15 22:41:16 +02:00
|
|
|
}
|
2025-01-06 12:50:39 +01:00
|
|
|
return defaultVal
|
2024-10-15 22:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to safely convert environment variables to uint
|
|
|
|
func GetEnvUint(key string, defaultVal uint) uint {
|
|
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
|
|
if parsed, err := strconv.Atoi(value); err == nil && parsed >= 0 {
|
|
|
|
return uint(parsed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|