u
This commit is contained in:
1 parent
d60be4a75b
commit
be8eaae7c5
24 files changed
+595
-232
No files matched your search
@@ -74,6 +74,7 @@ type middlewareEntry struct {
|
||||
|
||||
// 判断路径是否被排除
|
||||
func (m *middlewareEntry) IsExcluded(path string) bool {
|
||||
path = strings.TrimSuffix(path, "/") // 去掉尾部斜杠
|
||||
if m.excludePaths != nil {
|
||||
if _, ok := m.excludePaths[path]; ok {
|
||||
return true
|
||||
@@ -123,23 +124,36 @@ func (r *Router) Use(handler HandlerFunc) *MiddlewareHandle {
|
||||
}
|
||||
|
||||
// ExcludePaths 支持 /** 前缀匹配
|
||||
func (mh *MiddlewareHandle) ExcludePaths(paths ...string) *Router {
|
||||
excludeMap := make(map[string]struct{}, 0)
|
||||
// ExcludePaths 支持 /** 前缀匹配,并自动带上 basePath
|
||||
func (mh *MiddlewareHandle) ExcludePaths(paths ...string) *MiddlewareHandle {
|
||||
excludeMap := make(map[string]struct{})
|
||||
excludePrefix := make([]string, 0)
|
||||
|
||||
basePath := strings.TrimSuffix(mh.router.basePath, "/") // 去掉尾部斜杠
|
||||
|
||||
for _, p := range paths {
|
||||
if strings.HasSuffix(p, "/**") {
|
||||
base := strings.TrimSuffix(p, "/**")
|
||||
fullPath := p
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
fullPath = "/" + p
|
||||
}
|
||||
|
||||
// 自动加上 basePath
|
||||
if basePath != "/" {
|
||||
fullPath = basePath + fullPath
|
||||
}
|
||||
|
||||
if strings.HasSuffix(fullPath, "/**") {
|
||||
base := strings.TrimSuffix(fullPath, "/**")
|
||||
excludePrefix = append(excludePrefix, base)
|
||||
} else {
|
||||
excludeMap[p] = struct{}{}
|
||||
excludeMap[fullPath] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
mh.router.middleware[mh.entryIdx].excludePaths = excludeMap
|
||||
mh.router.middleware[mh.entryIdx].excludePrefix = excludePrefix
|
||||
|
||||
return mh.router
|
||||
return mh
|
||||
}
|
||||
|
||||
// Group 创建子路由组
|
||||
|
||||
Reference in new issue
Block a user