middleware.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package middleware
  2. import "github.com/gogf/gf/v2/net/ghttp"
  3. // EmptyMiddleware 不执行业务处理的中间件
  4. func EmptyMiddleware(r *ghttp.Request) {
  5. r.Middleware.Next()
  6. }
  7. type SkipperFunc func(request *ghttp.Request) bool
  8. // AllowPathPrefixSkipper 检查请求路径是否包含指定的前缀,如果包含则跳过
  9. func AllowPathPrefixSkipper(prefixes ...string) SkipperFunc {
  10. return func(request *ghttp.Request) bool {
  11. path := request.URL.Path
  12. pathLen := len(path)
  13. for _, p := range prefixes {
  14. if pl := len(p); pathLen >= pl && path[:pl] == p {
  15. return true
  16. }
  17. }
  18. return false
  19. }
  20. }
  21. // AllowPathPrefixNoSkipper 检查请求路径是否包含指定的前缀,如果包含则不跳过
  22. func AllowPathPrefixNoSkipper(prefixes ...string) SkipperFunc {
  23. return func(request *ghttp.Request) bool {
  24. path := request.URL.Path
  25. pathLen := len(path)
  26. for _, p := range prefixes {
  27. if pl := len(p); pathLen >= pl && path[:pl] == p {
  28. return false
  29. }
  30. }
  31. return true
  32. }
  33. }
  34. // SkipHandler 统一处理跳过函数
  35. func SkipHandler(r *ghttp.Request, skippers ...SkipperFunc) bool {
  36. for _, skipper := range skippers {
  37. if skipper(r) {
  38. return true
  39. }
  40. }
  41. return false
  42. }