From 39cecc80d149fd079836a5fc308c75b5c548ebfe Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 08:43:41 +0300 Subject: [PATCH] Add Middleware to routes and update Swagger to add that --- cmd/http-server/main.go | 5 +++-- handlers/card.go | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index 1bd2cb5..8e4620d 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -6,6 +6,7 @@ import ( docs "git.qowevisa.me/Qowevisa/gonuts/docs" "git.qowevisa.me/Qowevisa/gonuts/handlers" + "git.qowevisa.me/Qowevisa/gonuts/middleware" "git.qowevisa.me/Qowevisa/gonuts/tokens" "github.com/gin-gonic/gin" ) @@ -36,10 +37,10 @@ func main() { userRoutes.POST("/register", handlers.UserRegister) userRoutes.POST("/login", handlers.UserLogin) } - cardsRoutes := api.Group("/card") + cardsRoutes := api.Group("/card", middleware.AuthMiddleware()) { cardsRoutes.GET("/:id", handlers.CardGetId) - cardsRoutes.POST("/add", handlers.CardGetId) + cardsRoutes.POST("/add", handlers.CardAdd) } } diff --git a/handlers/card.go b/handlers/card.go index 4532508..14dd023 100644 --- a/handlers/card.go +++ b/handlers/card.go @@ -14,10 +14,12 @@ import ( // @Tags card // @Accept json // @Produce json -// @Param card param int true "id" +// @Param Authorization header string true "Bearer token" +// @Param card path int true "id" // @Success 200 {object} types.DbCard // @Failure 400 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse +// @Security ApiKeyAuth // @Router /card/:id [get] func CardGetId(c *gin.Context) { idStr := c.Param("id") @@ -53,11 +55,13 @@ func CardGetId(c *gin.Context) { // @Tags card // @Accept json // @Produce json +// @Param Authorization header string true "Bearer token" // @Param card body types.DbCard true "Card" // @Success 200 {object} types.Message // @Failure 400 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse -// @Router /card/:id [get] +// @Security ApiKeyAuth +// @Router /card/add [post] func CardAdd(c *gin.Context) { var card types.DbCard if err := c.ShouldBindJSON(&card); err != nil { @@ -85,4 +89,3 @@ func CardAdd(c *gin.Context) { } c.JSON(200, msg) } -