mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
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]>
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2025 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"github.com/hanzoai/git/modules/web"
|
|
"net/http"
|
|
"strings"
|
|
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/reqctx"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/web/middleware"
|
|
"github.com/hanzoai/git/modules/web/routing"
|
|
)
|
|
|
|
func BlockExpensive() func(next http.Handler) http.Handler {
|
|
if !setting.Service.BlockAnonymousAccessExpensive {
|
|
return nil
|
|
}
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
ret := determineRequestPriority(reqctx.FromContext(req.Context()))
|
|
if !ret.SignedIn {
|
|
if ret.Expensive || ret.LongPolling {
|
|
http.Redirect(w, req, middleware.RedirectLinkUserLogin(req), http.StatusSeeOther)
|
|
return
|
|
}
|
|
}
|
|
next.ServeHTTP(w, req)
|
|
})
|
|
}
|
|
}
|
|
|
|
func isRoutePathExpensive(routePattern string) bool {
|
|
if strings.HasPrefix(routePattern, "/user/") || strings.HasPrefix(routePattern, "/login/") {
|
|
return false
|
|
}
|
|
|
|
expensivePaths := []string{
|
|
// code related
|
|
"/{username}/{reponame}/archive/",
|
|
"/{username}/{reponame}/blame/",
|
|
"/{username}/{reponame}/commit/",
|
|
"/{username}/{reponame}/commits/",
|
|
"/{username}/{reponame}/compare/",
|
|
"/{username}/{reponame}/graph",
|
|
"/{username}/{reponame}/media/",
|
|
"/{username}/{reponame}/raw/",
|
|
"/{username}/{reponame}/rss/branch/",
|
|
"/{username}/{reponame}/src/",
|
|
|
|
// issue & PR related (no trailing slash)
|
|
"/{username}/{reponame}/issues",
|
|
"/{username}/{reponame}/{type:issues}",
|
|
"/{username}/{reponame}/pulls",
|
|
"/{username}/{reponame}/{type:pulls}",
|
|
|
|
// wiki
|
|
"/{username}/{reponame}/wiki/",
|
|
|
|
// activity
|
|
"/{username}/{reponame}/activity/",
|
|
}
|
|
for _, path := range expensivePaths {
|
|
if strings.HasPrefix(routePattern, path) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func determineRequestPriority(reqCtx reqctx.RequestContext) (ret struct {
|
|
SignedIn bool
|
|
Expensive bool
|
|
LongPolling bool
|
|
},
|
|
) {
|
|
chiRoutePath := web.GetRouteContext(reqCtx).RoutePattern()
|
|
if _, ok := reqCtx.GetData()[middleware.ContextDataKeySignedUser].(*user_model.User); ok {
|
|
ret.SignedIn = true
|
|
} else {
|
|
ret.Expensive = isRoutePathExpensive(chiRoutePath)
|
|
ret.LongPolling = routing.GetRequestRecordInfo(reqCtx).IsLongPolling
|
|
}
|
|
return ret
|
|
}
|