lolly/internal/matcher/prefix_priority.go
xfy 53eaec57ad feat(matcher): 添加 nginx 风格 location 匹配引擎
实现 nginx 兼容的 location 匹配系统,支持:
- 精确匹配 (=) - Hash Map O(1)
- 前缀优先匹配 (^~) - Radix Tree
- 正则匹配 (~, ~*) - 按配置顺序
- 普通前缀匹配 - Radix Tree 最长匹配
- 命名 location (@name) - 内部重定向

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 09:26:22 +08:00

33 lines
903 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package matcher
import "github.com/valyala/fasthttp"
// PrefixPriorityMatcher ^~ 类型前缀优先匹配器(封装 RadixTree
type PrefixPriorityMatcher struct {
tree *RadixTree
priority int
}
// NewPrefixPriorityMatcher 创建前缀优先匹配器
func NewPrefixPriorityMatcher() *PrefixPriorityMatcher {
return &PrefixPriorityMatcher{
tree: NewRadixTree(),
priority: 2, // ^~ 类型优先级更高
}
}
// AddPath 添加路径
func (ppm *PrefixPriorityMatcher) AddPath(path string, handler fasthttp.RequestHandler) error {
return ppm.tree.Insert(path, handler, ppm.priority)
}
// Match 前缀优先匹配,返回最长前缀匹配结果
func (ppm *PrefixPriorityMatcher) Match(path string) *MatchResult {
return ppm.tree.FindLongestPrefix(path)
}
// MarkInitialized 标记初始化完成
func (ppm *PrefixPriorityMatcher) MarkInitialized() {
ppm.tree.MarkInitialized()
}