Move ENV imports to config package

This commit is contained in:
zardzul
2026-03-16 19:08:44 +01:00
parent 6bff66b31b
commit 563f9ac08d
3 changed files with 59 additions and 35 deletions
+50
View File
@@ -0,0 +1,50 @@
package utils
import (
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
JWTsecret string
JWTIssuer string
JWTTTL int
DBURL string
}
func LoadConfig() (*Config, error) {
Config := new(Config)
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
Config.JWTsecret = os.Getenv("JWT_SECRET")
if Config.JWTsecret == "" {
panic("JWT_SECRET is required")
}
Config.JWTIssuer = os.Getenv("JWT_ISSUER")
if Config.JWTIssuer == "" {
Config.JWTIssuer = "music-index-api"
}
Config.JWTTTL = 60
if envTTL := os.Getenv("JWT_TTL_MINUTES"); envTTL != "" {
parsed, parseErr := strconv.Atoi(envTTL)
if parseErr != nil || parsed <= 0 {
panic("JWT_TTL_MINUTES must be a positive integer")
}
Config.JWTTTL = parsed
}
Config.DBURL = os.Getenv("DATABASE_URL")
if Config.DBURL == "" {
panic("DATABASE_URL environment variable not set")
}
return Config, nil
}