90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package v1
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"api-service/internal/config"
|
|
"api-service/internal/handlers"
|
|
"api-service/internal/middleware"
|
|
"api-service/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
)
|
|
|
|
// RegisterRoutes registers all API routes for version 1
|
|
func RegisterRoutes(cfg *config.Config) *gin.Engine {
|
|
router := gin.New()
|
|
|
|
// Add middleware
|
|
router.Use(middleware.CORSConfig())
|
|
router.Use(middleware.ErrorHandler())
|
|
router.Use(gin.Logger())
|
|
router.Use(gin.Recovery())
|
|
|
|
// Initialize services
|
|
authService := services.NewAuthService(cfg)
|
|
|
|
// Swagger UI route
|
|
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
|
|
// API v1 group
|
|
v1 := router.Group("/api/v1")
|
|
{
|
|
// Public routes (no authentication required)
|
|
// Health endpoints
|
|
healthHandler := handlers.NewHealthHandler()
|
|
v1.GET("/health", healthHandler.GetHealth)
|
|
v1.GET("/", healthHandler.HelloWorld)
|
|
|
|
// Authentication routes
|
|
authHandler := handlers.NewAuthHandler(authService)
|
|
tokenHandler := handlers.NewTokenHandler(authService)
|
|
|
|
v1.POST("/auth/login", authHandler.Login)
|
|
v1.POST("/auth/register", authHandler.Register)
|
|
v1.GET("/auth/me", middleware.JWTAuthMiddleware(authService), authHandler.Me)
|
|
v1.POST("/auth/refresh", authHandler.RefreshToken)
|
|
|
|
// Token generation routes
|
|
v1.POST("/token/generate", tokenHandler.GenerateToken)
|
|
v1.POST("/token/generate-direct", tokenHandler.GenerateTokenDirect)
|
|
|
|
// Protected routes (require authentication)
|
|
protected := v1.Group("/")
|
|
protected.Use(middleware.JWTAuthMiddleware(authService))
|
|
{
|
|
// Product endpoints
|
|
productHandler := handlers.NewProductHandler()
|
|
protected.GET("/products", productHandler.GetProduct)
|
|
protected.GET("/products/:id", productHandler.GetProductByID)
|
|
protected.POST("/products", productHandler.CreateProduct)
|
|
protected.PUT("/products/:id", productHandler.UpdateProduct)
|
|
protected.DELETE("/products/:id", productHandler.DeleteProduct)
|
|
|
|
// Example endpoints
|
|
exampleHandler := handlers.NewExampleHandler()
|
|
protected.GET("/example", exampleHandler.GetExample)
|
|
protected.POST("/example", exampleHandler.PostExample)
|
|
|
|
// WebSocket endpoint
|
|
protected.GET("/websocket", WebSocketHandler)
|
|
protected.GET("/webservice", WebServiceHandler)
|
|
}
|
|
}
|
|
|
|
return router
|
|
}
|
|
|
|
// WebSocketHandler handles WebSocket connections
|
|
func WebSocketHandler(c *gin.Context) {
|
|
// This will be implemented with proper WebSocket handling
|
|
c.JSON(http.StatusOK, gin.H{"message": "WebSocket endpoint"})
|
|
}
|
|
|
|
func WebServiceHandler(c *gin.Context) {
|
|
// This will be implemented with proper WebSocket handling
|
|
c.JSON(http.StatusOK, gin.H{"message": "WebSocket endpoint"})
|
|
}
|