v1.3.0: BindRoute wildcard routes (/prefix/*) with RouteCtx.Wildcard

This commit is contained in:
2026-08-20 21:10:24 +08:00
parent e6c6f68a3a
commit 503d0904d5
3 changed files with 51 additions and 18 deletions
+22 -6
View File
@@ -16,10 +16,12 @@ type RouteHandler func(ctx *RouteCtx) error
// RouteCtx 自定义路由上下文:Data 合并了 URL 查询参数与 POST 表单参数(表单优先),
// 支付回调等第三方以 form-urlencoded 回调的场景可直接 Param 取值。
// Wildcard 为通配符路由(/prefix/*)匹配到的剩余路径(不含前导 "/")。
// 独立于服务内嵌的 Ctx:后者辅助方法刻意全小写以防混入 RPC 方法集,路由不复用该类型。
type RouteCtx struct {
RequestCtx *fasthttp.RequestCtx
Data map[string]string
Wildcard string
}
// Param 取查询/表单参数,不存在返回空串
@@ -27,10 +29,12 @@ func (c *RouteCtx) Param(name string) string {
return c.Data[name]
}
// BindRoute 注册自定义路由(方法大小写不敏感 + 精确路径匹配),用于 GET 直链、
// 健康检查、支付回调等无法走 POST /cell RPC 的场景。
// BindRoute 注册自定义路由(方法大小写不敏感path 精确匹配,或以 "/*" 结尾做前缀通配),
// 用于 GET 直链、健康检查、支付回调等无法走 POST /cell RPC 的场景。
//
// - path 必须以 "/" 开头;/cell 为 RPC 保留路径,不可注册
// - 通配符形式如 "/image/*":匹配 "/image/a/b.png" 等任意子路径,
// 匹配到的剩余路径(去掉前导 "/",如 "a/b.png")经 RouteCtx.Wildcard 取出
// - 同一 方法+路径 重复注册直接 panic
// - 与 BindService 一致,需在 Start 前完成注册(启动阶段单线程)
func (f *Fun) BindRoute(method, path string, handler RouteHandler) {
@@ -44,9 +48,21 @@ func (f *Fun) BindRoute(method, path string, handler RouteHandler) {
if !strings.HasPrefix(path, "/") {
panic(fmt.Sprintf("fun: BindRoute path %q must start with '/'", path))
}
if path == "/cell" {
if path == "/cell" || path == "/cell/*" {
panic("fun: /cell is reserved for RPC")
}
if prefix, ok := strings.CutSuffix(path, "/*"); ok {
if prefix == "" || strings.HasSuffix(prefix, "/") {
panic(fmt.Sprintf("fun: BindRoute wildcard path %q invalid (no trailing '/' allowed before /*)", path))
}
for _, r := range f.wildcardRoutes[method] {
if r.prefix == prefix {
panic(fmt.Sprintf("fun: route %s %s/* already bound", method, prefix))
}
}
f.wildcardRoutes[method] = append(f.wildcardRoutes[method], wildcardRoute{prefix: prefix, handler: handler})
return
}
key := method + " " + path
if _, exists := f.routes[key]; exists {
panic(fmt.Sprintf("fun: route %s already bound", key))
@@ -55,8 +71,8 @@ func (f *Fun) BindRoute(method, path string, handler RouteHandler) {
}
// handleRoute 执行自定义路由:合并查询与表单参数(application/x-www-form-urlencoded),
// 处理器返回 error 时按统一 Result 格式输出错误响应
func (f *Fun) handleRoute(fastCtx *fasthttp.RequestCtx, handler RouteHandler) {
// 处理器返回 error 时按统一 Result 格式输出错误响应;wildcard 为通配路由匹配的剩余路径
func (f *Fun) handleRoute(fastCtx *fasthttp.RequestCtx, handler RouteHandler, wildcard string) {
data := map[string]string{}
fastCtx.QueryArgs().VisitAll(func(k, v []byte) {
data[string(k)] = string(v)
@@ -64,7 +80,7 @@ func (f *Fun) handleRoute(fastCtx *fasthttp.RequestCtx, handler RouteHandler) {
fastCtx.PostArgs().VisitAll(func(k, v []byte) {
data[string(k)] = string(v)
})
if err := handler(&RouteCtx{RequestCtx: fastCtx, Data: data}); err != nil {
if err := handler(&RouteCtx{RequestCtx: fastCtx, Data: data, Wildcard: wildcard}); err != nil {
(&Ctx{RequestCtx: fastCtx}).sendError(err)
}
}