为 matcher 包所有文件添加完整文档注释: - conflict: 路径冲突检测器 - exact: 精确路径匹配器(O(1) hash map) - location: 统一匹配引擎(整合所有策略) - matcher: 匹配结果和接口定义 - named: 命名捕获组匹配器 - prefix: 普通前缀匹配器 - prefix_priority: 前缀优先匹配器(^~) - radix: Radix Tree 最长前缀匹配 - regex: 正则表达式匹配器 注释说明匹配优先级顺序(精确 > 前缀优先 > 正则 > 普通前缀), 以及各匹配器的使用方法和性能特点。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
// Package matcher 提供 nginx 风格的 location 匹配引擎实现。
|
||
//
|
||
// 该文件实现精确路径匹配器,使用 hash map 实现 O(1) 查找。
|
||
//
|
||
// 作者:xfy
|
||
package matcher
|
||
|
||
import "github.com/valyala/fasthttp"
|
||
|
||
// ExactMatcher Hash Map 精确匹配器。
|
||
//
|
||
// 通过字符串等值比较实现 O(1) 时间复杂度的路径匹配,
|
||
// 对应 nginx 的 = 修饰符。
|
||
type ExactMatcher struct {
|
||
// handler 请求处理器
|
||
handler fasthttp.RequestHandler
|
||
|
||
// path 精确匹配路径
|
||
path string
|
||
|
||
// priority 匹配优先级,精确匹配为 1(最高)
|
||
priority int
|
||
}
|
||
|
||
// NewExactMatcher 创建精确匹配器。
|
||
//
|
||
// 参数:
|
||
// - path: 精确匹配的路径
|
||
// - handler: 匹配成功后的请求处理器
|
||
// - priority: 优先级(通常设为 1)
|
||
//
|
||
// 返回值:
|
||
// - *ExactMatcher: 精确匹配器实例
|
||
func NewExactMatcher(path string, handler fasthttp.RequestHandler, priority int) *ExactMatcher {
|
||
return &ExactMatcher{
|
||
path: path,
|
||
handler: handler,
|
||
priority: priority,
|
||
}
|
||
}
|
||
|
||
// Match 检查路径是否精确匹配。
|
||
//
|
||
// 参数:
|
||
// - path: 待检查的请求路径
|
||
//
|
||
// 返回值:
|
||
// - bool: 路径完全相等时返回 true
|
||
func (m *ExactMatcher) Match(path string) bool {
|
||
return m.path == path
|
||
}
|
||
|
||
// Result 返回匹配结果。
|
||
//
|
||
// 返回值:
|
||
// - *MatchResult: 包含处理器和元数据的匹配结果
|
||
func (m *ExactMatcher) Result() *MatchResult {
|
||
return &MatchResult{
|
||
Handler: m.handler,
|
||
Path: m.path,
|
||
Priority: m.priority,
|
||
LocationType: LocationTypeExact,
|
||
}
|
||
}
|