// Copyright (c) 2025 Nikolai Papin // // This file is part of Easywish // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package controllers import ( "easywish/internal/dto" "easywish/internal/services" "easywish/internal/utils/enums" "easywish/internal/validation" "net/http" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "go.uber.org/zap" ) var ( GET = "GET" POST = "POST" PUT = "PUT" PATCH = "PATCH" DELETE = "DELETE" ) type ControllerMethod struct { HttpMethod string Path string Authorization enums.Role Middleware []gin.HandlerFunc Function func (c *gin.Context) } type controllerImpl struct { Path string Middleware []gin.HandlerFunc Methods []ControllerMethod } type Controller interface { Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) } func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) { ctrlGroup := group.Group(ctrl.Path) ctrlGroup.Use(ctrl.Middleware...) for _, method := range ctrl.Methods { ctrlGroup.Handle( method.HttpMethod, method.Path, append( method.Middleware, gin.HandlerFunc(func(c *gin.Context) { clientInfo, _ := c.Get("client_info") if clientInfo.(dto.ClientInfo).Role < method.Authorization { c.AbortWithStatusJSON( http.StatusForbidden, gin.H{"error": "Insufficient authorization for this method"}) return } }), method.Function)..., )} } func GetRequest[ModelT any](c *gin.Context) (*dto.Request[ModelT], error) { var body ModelT if err := c.ShouldBindJSON(&body); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return nil, err } // TODO: Think hard on a singleton for better performance validate := validation.NewValidator() if err := validate.Struct(body); err != nil { errorList := err.(validator.ValidationErrors) c.AbortWithStatusJSON( http.StatusBadRequest, gin.H{"error": errorList}) return nil, err } cinfoFromCtx, ok := c.Get("client_info"); if !ok { c.AbortWithStatusJSON( http.StatusInternalServerError, gin.H{"error": "Client info was not found"}) panic("No client_info found in gin context. Does the handler use AuthMiddleware?") } cinfo := cinfoFromCtx.(dto.ClientInfo) return &dto.Request[ModelT]{ Body: body, User: cinfo, }, nil }