package router import ( "exam_system/result" "github.com/gin-gonic/gin" "net/http" ) /** * @creator: xuwuruoshui * @date: 2022-06-08 22:27:02 * @content: router封装 */ type RouterPlus struct { router *gin.Engine } func NewRouterPlus(engine *gin.Engine) *RouterPlus { return &RouterPlus{ router: engine, } } type RouterHandler func(ctx *gin.Context) *result.Result func (r *RouterPlus) GET(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.router.GET(path, handlers...) } func (r *RouterPlus) POST(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.router.POST(path, handlers...) } func (r *RouterPlus) PUT(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.router.PUT(path, handlers...) } func (r *RouterPlus) DELETE(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.router.DELETE(path, handlers...) } func (r *RouterPlus) Group(path string, handlers ...gin.HandlerFunc) *RouterGroupPlus { return &RouterGroupPlus{ routerGroup: r.router.Group(path, handlers...), } } type RouterGroupPlus struct { routerGroup *gin.RouterGroup } func (r *RouterGroupPlus) GET(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.routerGroup.GET(path, handlers...) } func (r *RouterGroupPlus) POST(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.routerGroup.POST(path, handlers...) } func (r *RouterGroupPlus) PUT(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.routerGroup.PUT(path, handlers...) } func (r *RouterGroupPlus) DELETE(path string, handler RouterHandler, handlers ...gin.HandlerFunc) { postHanlder := PostHandler(handler) handlers = append(handlers, postHanlder) r.routerGroup.DELETE(path, handlers...) } // PostHandler 默认返回200 func PostHandler(handler RouterHandler) gin.HandlerFunc { return func(ctx *gin.Context) { ctx.JSON(http.StatusOK, handler(ctx)) } }