12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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))
- }
- }
|