65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
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"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
// @title Music Index API
|
|
// @version 1.0
|
|
// @description API for managing users and artists in Music Index.
|
|
// @BasePath /api/v1/
|
|
|
|
func main() {
|
|
err := godotenv.Load(".env")
|
|
if err != nil {
|
|
panic("Error loading .env file")
|
|
}
|
|
|
|
pool, queries, databaseError := database.Connect()
|
|
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)
|
|
artistRepo := repository.NewArtistRepository(queries)
|
|
artistHandler := handlers.NewArtistHandler(artistRepo)
|
|
|
|
router := gin.Default()
|
|
routes.Routes(router, userHandler, artistHandler, jwtSecret)
|
|
|
|
if routerError := router.Run(":8080"); routerError != nil {
|
|
panic(routerError)
|
|
}
|
|
}
|