实现 nginx 兼容的 location 匹配系统,支持: - 精确匹配 (=) - Hash Map O(1) - 前缀优先匹配 (^~) - Radix Tree - 正则匹配 (~, ~*) - 按配置顺序 - 普通前缀匹配 - Radix Tree 最长匹配 - 命名 location (@name) - 内部重定向 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
38 lines
801 B
Go
38 lines
801 B
Go
package matcher
|
|
|
|
import "github.com/valyala/fasthttp"
|
|
|
|
// NamedMatcher @命名 location
|
|
type NamedMatcher struct {
|
|
name string
|
|
handler fasthttp.RequestHandler
|
|
}
|
|
|
|
// NewNamedMatcher 创建命名匹配器
|
|
func NewNamedMatcher(name string, handler fasthttp.RequestHandler) *NamedMatcher {
|
|
return &NamedMatcher{
|
|
name: name,
|
|
handler: handler,
|
|
}
|
|
}
|
|
|
|
// Match 检查命名是否匹配(命名 location 不使用 path 匹配)
|
|
func (m *NamedMatcher) Match(path string) bool {
|
|
return false
|
|
}
|
|
|
|
// Result 返回匹配结果
|
|
func (m *NamedMatcher) Result() *MatchResult {
|
|
return &MatchResult{
|
|
Handler: m.handler,
|
|
Path: "@" + m.name,
|
|
Priority: 0,
|
|
LocationType: "named",
|
|
}
|
|
}
|
|
|
|
// Name 返回命名 location 的名称
|
|
func (m *NamedMatcher) Name() string {
|
|
return m.name
|
|
}
|