u
This commit is contained in:
1 parent
6849579652
commit
d60be4a75b
24 files changed
+463
-179
No files matched your search
@@ -67,8 +67,24 @@ func (n *node) search(parts []string, height int, params map[string]string) *nod
|
||||
|
||||
// 中间件条目
|
||||
type middlewareEntry struct {
|
||||
handler HandlerFunc
|
||||
excludePaths map[string]struct{}
|
||||
handler HandlerFunc
|
||||
excludePaths map[string]struct{}
|
||||
excludePrefix []string
|
||||
}
|
||||
|
||||
// 判断路径是否被排除
|
||||
func (m *middlewareEntry) IsExcluded(path string) bool {
|
||||
if m.excludePaths != nil {
|
||||
if _, ok := m.excludePaths[path]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, prefix := range m.excludePrefix {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Router 主结构
|
||||
@@ -78,7 +94,7 @@ type Router struct {
|
||||
basePath string
|
||||
}
|
||||
|
||||
// NewRouter 创建
|
||||
// 创建 Router
|
||||
func NewRouter(basePath ...string) *Router {
|
||||
path := "/"
|
||||
if len(basePath) > 0 && basePath[0] != "" {
|
||||
@@ -90,11 +106,13 @@ func NewRouter(basePath ...string) *Router {
|
||||
}
|
||||
}
|
||||
|
||||
// MiddlewareHandle 用于链式排除路径
|
||||
type MiddlewareHandle struct {
|
||||
router *Router
|
||||
entryIdx int
|
||||
}
|
||||
|
||||
// Use 注册中间件
|
||||
func (r *Router) Use(handler HandlerFunc) *MiddlewareHandle {
|
||||
entry := middlewareEntry{handler: handler}
|
||||
r.middleware = append(r.middleware, entry)
|
||||
@@ -104,15 +122,27 @@ func (r *Router) Use(handler HandlerFunc) *MiddlewareHandle {
|
||||
}
|
||||
}
|
||||
|
||||
// ExcludePaths 支持 /** 前缀匹配
|
||||
func (mh *MiddlewareHandle) ExcludePaths(paths ...string) *Router {
|
||||
excludeMap := make(map[string]struct{}, len(paths))
|
||||
excludeMap := make(map[string]struct{}, 0)
|
||||
excludePrefix := make([]string, 0)
|
||||
|
||||
for _, p := range paths {
|
||||
excludeMap[p] = struct{}{}
|
||||
if strings.HasSuffix(p, "/**") {
|
||||
base := strings.TrimSuffix(p, "/**")
|
||||
excludePrefix = append(excludePrefix, base)
|
||||
} else {
|
||||
excludeMap[p] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
mh.router.middleware[mh.entryIdx].excludePaths = excludeMap
|
||||
mh.router.middleware[mh.entryIdx].excludePrefix = excludePrefix
|
||||
|
||||
return mh.router
|
||||
}
|
||||
|
||||
// Group 创建子路由组
|
||||
func (r *Router) Group(prefix string, m ...HandlerFunc) *Router {
|
||||
newMiddleware := make([]middlewareEntry, len(r.middleware))
|
||||
copy(newMiddleware, r.middleware)
|
||||
@@ -127,6 +157,7 @@ func (r *Router) Group(prefix string, m ...HandlerFunc) *Router {
|
||||
}
|
||||
}
|
||||
|
||||
// GET/POST
|
||||
func (r *Router) GET(path string, handler HandlerFunc) *Router {
|
||||
return r.handle("GET", path, handler)
|
||||
}
|
||||
@@ -145,7 +176,7 @@ func (r *Router) handle(method, path string, handler HandlerFunc) *Router {
|
||||
return r
|
||||
}
|
||||
|
||||
// Handler 入口函数(fasthttp)
|
||||
// Handler 入口(fasthttp)
|
||||
func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
|
||||
method := string(ctx.Method())
|
||||
root := r.roots[method]
|
||||
@@ -166,10 +197,8 @@ func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
|
||||
handlers := make([]HandlerFunc, 0)
|
||||
path := string(ctx.Path())
|
||||
for _, m := range r.middleware {
|
||||
if m.excludePaths != nil {
|
||||
if _, excluded := m.excludePaths[path]; excluded {
|
||||
continue
|
||||
}
|
||||
if m.IsExcluded(path) {
|
||||
continue
|
||||
}
|
||||
handlers = append(handlers, m.handler)
|
||||
}
|
||||
@@ -185,6 +214,7 @@ func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
|
||||
_ = c.Next()
|
||||
}
|
||||
|
||||
// 辅助函数
|
||||
func parsePattern(pattern string) []string {
|
||||
vs := strings.Split(strings.Trim(pattern, "/"), "/")
|
||||
parts := make([]string, 0, len(vs))
|
||||
|
||||
Reference in new issue
Block a user