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
+7 -27
View File
@@ -1,17 +1,15 @@
package main
import (
"os"
"strconv"
"time"
"zardzul/music-index/database"
_ "zardzul/music-index/docs"
"zardzul/music-index/handlers"
"zardzul/music-index/repository"
"zardzul/music-index/routes"
"zardzul/music-index/utils"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
// @title Music Index API
@@ -20,43 +18,25 @@ import (
// @BasePath /api/v1/
func main() {
err := godotenv.Load(".env")
config, err := utils.LoadConfig()
if err != nil {
panic("Error loading .env file")
return
}
pool, queries, databaseError := database.Connect()
pool, queries, databaseError := database.Connect(config.DBURL)
if databaseError != nil {
panic(databaseError)
}
defer pool.Close()
jwtSecret := os.Getenv("JWT_SECRET")
if jwtSecret == "" {
panic("JWT_SECRET is required")
}
jwtIssuer := os.Getenv("JWT_ISSUER")
if jwtIssuer == "" {
jwtIssuer = "music-index-api"
}
jwtTTLMinutes := 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")
}
jwtTTLMinutes = parsed
}
userRepo := repository.NewUserRepository(queries)
userHandler := handlers.NewUserHandler(userRepo, jwtSecret, jwtIssuer, time.Duration(jwtTTLMinutes)*time.Minute)
userHandler := handlers.NewUserHandler(userRepo, config.JWTsecret, config.JWTIssuer, time.Duration(config.JWTTTL)*time.Minute)
artistRepo := repository.NewArtistRepository(queries)
artistHandler := handlers.NewArtistHandler(artistRepo)
router := gin.Default()
routes.Routes(router, userHandler, artistHandler, jwtSecret)
routes.Routes(router, userHandler, artistHandler, config.JWTsecret)
if routerError := router.Run(":8080"); routerError != nil {
panic(routerError)