router-plus.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package router
  2. import (
  3. "exam_system/result"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. /**
  8. * @creator: xuwuruoshui
  9. * @date: 2022-06-08 22:27:02
  10. * @content: router封装
  11. */
  12. type RouterPlus struct {
  13. router *gin.Engine
  14. }
  15. func NewRouterPlus(engine *gin.Engine) *RouterPlus {
  16. return &RouterPlus{
  17. router: engine,
  18. }
  19. }
  20. type RouterHandler func(ctx *gin.Context) *result.Result
  21. func (r *RouterPlus) GET(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  22. postHanlder := PostHandler(handler)
  23. handlers = append(handlers, postHanlder)
  24. r.router.GET(path, handlers...)
  25. }
  26. func (r *RouterPlus) POST(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  27. postHanlder := PostHandler(handler)
  28. handlers = append(handlers, postHanlder)
  29. r.router.POST(path, handlers...)
  30. }
  31. func (r *RouterPlus) PUT(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  32. postHanlder := PostHandler(handler)
  33. handlers = append(handlers, postHanlder)
  34. r.router.PUT(path, handlers...)
  35. }
  36. func (r *RouterPlus) DELETE(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  37. postHanlder := PostHandler(handler)
  38. handlers = append(handlers, postHanlder)
  39. r.router.DELETE(path, handlers...)
  40. }
  41. func (r *RouterPlus) Group(path string, handlers ...gin.HandlerFunc) *RouterGroupPlus {
  42. return &RouterGroupPlus{
  43. routerGroup: r.router.Group(path, handlers...),
  44. }
  45. }
  46. type RouterGroupPlus struct {
  47. routerGroup *gin.RouterGroup
  48. }
  49. func (r *RouterGroupPlus) GET(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  50. postHanlder := PostHandler(handler)
  51. handlers = append(handlers, postHanlder)
  52. r.routerGroup.GET(path, handlers...)
  53. }
  54. func (r *RouterGroupPlus) POST(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  55. postHanlder := PostHandler(handler)
  56. handlers = append(handlers, postHanlder)
  57. r.routerGroup.POST(path, handlers...)
  58. }
  59. func (r *RouterGroupPlus) PUT(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  60. postHanlder := PostHandler(handler)
  61. handlers = append(handlers, postHanlder)
  62. r.routerGroup.PUT(path, handlers...)
  63. }
  64. func (r *RouterGroupPlus) DELETE(path string, handler RouterHandler, handlers ...gin.HandlerFunc) {
  65. postHanlder := PostHandler(handler)
  66. handlers = append(handlers, postHanlder)
  67. r.routerGroup.DELETE(path, handlers...)
  68. }
  69. // PostHandler 默认返回200
  70. func PostHandler(handler RouterHandler) gin.HandlerFunc {
  71. return func(ctx *gin.Context) {
  72. ctx.JSON(http.StatusOK, handler(ctx))
  73. }
  74. }