Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package webx
  2. import (
  3. "net/http"
  4. )
  5. // HandlerFunc 通用的处理器函数类型
  6. type HandlerFunc interface{}
  7. // Router 路由器
  8. type Router struct {
  9. mux *http.ServeMux
  10. globalBefore []func(http.Handler) http.Handler
  11. globalAfter []func(http.ResponseWriter, *http.Request)
  12. }
  13. func NewRouter(mux *http.ServeMux) *Router {
  14. return &Router{
  15. mux: mux,
  16. }
  17. }
  18. // UseBefore 在处理器前执行的中间件
  19. func (r *Router) UseBefore(middleware ...func(http.Handler) http.Handler) *Router {
  20. r.globalBefore = append(r.globalBefore, middleware...)
  21. return r
  22. }
  23. // UseAfter 在处理器后执行的中间件
  24. func (r *Router) UseAfter(middleware ...func(http.ResponseWriter, *http.Request)) *Router {
  25. r.globalAfter = append(r.globalAfter, middleware...)
  26. return r
  27. }
  28. // GET 注册 GET 请求
  29. func (r *Router) GET(path string, handler HandlerFunc) *routeBuilder {
  30. return r.handle("GET", path, handler)
  31. }
  32. // POST 注册 POST 请求
  33. func (r *Router) POST(path string, handler HandlerFunc) *routeBuilder {
  34. return r.handle("POST", path, handler)
  35. }
  36. // handle 内部处理方法
  37. func (r *Router) handle(method, path string, handler HandlerFunc) *routeBuilder {
  38. return &routeBuilder{
  39. router: r,
  40. method: method,
  41. path: path,
  42. handler: handler,
  43. }
  44. }
  45. // 路由构建器
  46. type routeBuilder struct {
  47. router *Router
  48. method string
  49. path string
  50. handler HandlerFunc
  51. before []func(http.Handler) http.Handler
  52. after []func(http.ResponseWriter, *http.Request)
  53. description string
  54. }
  55. // Before 添加路由前中间件
  56. func (rb *routeBuilder) Before(middleware ...func(http.Handler) http.Handler) *routeBuilder {
  57. rb.before = append(rb.before, middleware...)
  58. return rb
  59. }
  60. // After 添加路由后中间件
  61. func (rb *routeBuilder) After(middleware ...func(http.ResponseWriter, *http.Request)) *routeBuilder {
  62. rb.after = append(rb.after, middleware...)
  63. return rb
  64. }
  65. // Desc 添加描述
  66. func (rb *routeBuilder) Desc(desc string) *routeBuilder {
  67. rb.description = desc
  68. return rb
  69. }
  70. // Register 注册路由
  71. func (rb *routeBuilder) Register() {
  72. // 创建最终的 HTTP 处理器
  73. finalHandler := rb.createHandler()
  74. // 应用路由级 before 中间件
  75. for i := len(rb.before) - 1; i >= 0; i-- {
  76. finalHandler = rb.before[i](finalHandler)
  77. }
  78. // 应用全局 before 中间件
  79. for i := len(rb.router.globalBefore) - 1; i >= 0; i-- {
  80. finalHandler = rb.router.globalBefore[i](finalHandler)
  81. }
  82. // 注册到 ServeMux
  83. rb.router.mux.Handle(rb.path, finalHandler)
  84. }
  85. // 创建处理器
  86. func (rb *routeBuilder) createHandler() http.Handler {
  87. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  88. // 方法检查
  89. if rb.method != "" && req.Method != rb.method {
  90. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  91. return
  92. }
  93. // 创建上下文
  94. ctx := req.Context()
  95. // 执行处理器
  96. rb.invokeHandler(ctx, w, req)
  97. // 执行路由级 after 中间件
  98. for _, after := range rb.after {
  99. after(w, req)
  100. }
  101. // 执行全局 after 中间件
  102. for _, after := range rb.router.globalAfter {
  103. after(w, req)
  104. }
  105. })
  106. }