Files
forge/services/context/base_path.go
zeekayandHanzo Dev ee51373566 web: own the route context; chi is only the mux now
Path parameters were read through chi from all over the codebase. They are read
through modules/web.RouteContext now, and chi is confined to five files.

The realisation that made this small: chi was never doing the hard part.
router_path.go has always compiled our own patterns to regexps and captured
groups itself — the 48 regex-constrained routes ({sha:([a-f0-9]{7,64})},
{ball_type:tarball|zipball|bundle}) are ours, not chi features. chi was holding
three fields on the way through: routed method, captured params, matched
patterns. Depending on a router for those made the router look load-bearing.

  modules/web/route_context.go   the type: method, params, patterns
  router_path.go                 matchPath fills it directly; chi-free
  base_path.go                   PathParam/SetPathParam read and write it —
                                 the choke point every handler goes through
  qos, blockexpensive, tracing   read RoutePattern from it
  contexttest                    builds one, so a mock request looks routed

RoutePattern collapses the "/*" each Mount contributes, which is what chi did
and what makes the value the pattern a person would have written — logs and
metrics group by it.

What is still chi, and honestly: the mux in modules/web/router.go, the route
tables that feed it, and one BeforeRouting middleware that sets the path the mux
routes ON. Replacing it means a router for 1333 routes including those 48 regex
patterns — stdlib ServeMux cannot express them, and zip is fiber/fasthttp, so
zip-native means moving the whole web layer off net/http. That is a real
decision, not a rename, and it is now a decision about one file.

models, modules, services, routers green; integration compile gate green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 17:55:28 -07:00

54 lines
1.5 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package context
import (
"net/url"
"strconv"
"strings"
"github.com/hanzoai/git/modules/setting"
"github.com/hanzoai/git/modules/web"
)
// PathParam returns the param in request path, eg: "/{var}" => "/a%2fb", then `var == "a/b"`
func (b *Base) PathParam(name string) string {
s, err := url.PathUnescape(b.PathParamRaw(name))
if err != nil && !setting.IsProd {
panic("Failed to unescape path param: " + err.Error() + ", there seems to be a double-unescaping bug")
}
return s
}
// PathParamRaw returns the raw param in request path, eg: "/{var}" => "/a%2fb", then `var == "a%2fb"`
func (b *Base) PathParamRaw(name string) string {
if strings.HasPrefix(name, ":") {
setting.PanicInDevOrTesting("path param should not start with ':'")
name = name[1:]
}
return web.GetRouteContext(b.Req.Context()).Param(name)
}
// PathParamInt64 returns the param in request path as int64
func (b *Base) PathParamInt64(p string) int64 {
v, _ := strconv.ParseInt(b.PathParam(p), 10, 64)
return v
}
func (b *Base) PathParamInt(p string) int {
v, _ := strconv.Atoi(b.PathParam(p))
return v
}
// SetPathParam set request path params into routes
func (b *Base) SetPathParam(name, value string) {
web.GetRouteContext(b).SetParam(name, url.PathEscape(value))
}
func (b *Base) SetPathParamRaw(name, value string) {
web.GetRouteContext(b).SetParam(name, value)
}