Move ENV imports to config package
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user