package main import ( "bookmann/internal/config" "bookmann/internal/database" "bookmann/internal/handlers" "log" "github.com/go-playground/validator/v10" "github.com/golang-jwt/jwt/v5" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) // CustomValidator wraps the go-playground validator type CustomValidator struct { validator *validator.Validate } func (cv *CustomValidator) Validate(i interface{}) error { return cv.validator.Struct(i) } func main() { cfg := config.LoadConfig() pool, err := database.NewConnection(cfg.DatabaseURL()) if err != nil { log.Fatal("Failed to connect to database:", err) } defer pool.Close() queries := database.New(pool) e := echo.New() // Set up validator e.Validator = &CustomValidator{validator: validator.New()} // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) e.Use(middleware.CORS()) // Auth routes (no auth required) auth := handlers.NewAuthHandler(queries, cfg.JWTSecret) e.POST("/api/auth/register", auth.Register) e.POST("/api/auth/login", auth.Login) // JWT middleware for protected routes jwtMiddleware := middleware.JWTWithConfig(middleware.JWTConfig{ SigningKey: []byte(cfg.JWTSecret), ContextKey: "user", SuccessHandler: func(c echo.Context) { token := c.Get("user").(*jwt.Token) claims := token.Claims.(jwt.MapClaims) c.Set("user_id", claims["user_id"]) }, }) // Protected routes protected := e.Group("/api", jwtMiddleware) protected.GET("/auth/profile", auth.GetProfile) // Routes handlers.SetupRoutes(protected, queries) // Serve static files e.Static("/", "static") // Start server log.Printf("Starting server on port %s", cfg.ServerPort) e.Logger.Fatal(e.Start(":" + cfg.ServerPort)) }