-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 关于路径的匹配器 | ||
|
||
package matcher | ||
|
||
import ( | ||
"fmt" | ||
"knife" | ||
"log" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// PathPrefix 路径前缀匹配 | ||
func PathPrefix(prefix string) knife.MiddlewareMatcher { | ||
return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { | ||
return strings.HasPrefix(request.URL.Path, prefix) | ||
} | ||
} | ||
|
||
// PathMatch 路径正则匹配 | ||
func PathMatch(pattern string) knife.MiddlewareMatcher { | ||
return func(response knife.HttpResponseWriter, request knife.HttpRequest) bool { | ||
matched, err := isPathMatched(request.URL.Path, pattern) | ||
if err != nil { | ||
log.Println("path matched failed") | ||
return false | ||
} | ||
return matched | ||
} | ||
} | ||
|
||
// isPathMatched 路径匹配方式 | ||
// path 路径 | ||
// pattern 正则表达式 | ||
func isPathMatched(path string, pattern string) (bool, error) { | ||
re, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid regular expression: %w", err) | ||
} | ||
return re.MatchString(path), nil | ||
} |