diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-04-21 02:49:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-20 14:49:06 -0400 |
commit | b9a97ccd0ea1ee44db85b0fbb80b75255af7c742 (patch) | |
tree | 300578dc3c3e62a4cf956ccdc22b8b0ad0cc6036 /routers | |
parent | 70fc47a22a0bfaef7fb16dcc8a6a2e011b10f8d4 (diff) | |
download | gitea-b9a97ccd0ea1ee44db85b0fbb80b75255af7c742.tar.gz gitea-b9a97ccd0ea1ee44db85b0fbb80b75255af7c742.zip |
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/packages/api.go | 2 | ||||
-rw-r--r-- | routers/api/v1/api.go | 6 | ||||
-rw-r--r-- | routers/install/routes.go | 6 | ||||
-rw-r--r-- | routers/private/internal.go | 6 | ||||
-rw-r--r-- | routers/web/web.go | 10 |
5 files changed, 15 insertions, 15 deletions
diff --git a/routers/api/packages/api.go b/routers/api/packages/api.go index 4cebabecf0..8bf5dbab35 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -480,7 +480,7 @@ func ContainerRoutes(ctx gocontext.Context) *web.Route { ) // Manual mapping of routes because {image} can contain slashes which chi does not support - r.Route("/*", "HEAD,GET,POST,PUT,PATCH,DELETE", func(ctx *context.Context) { + r.RouteMethods("/*", "HEAD,GET,POST,PUT,PATCH,DELETE", func(ctx *context.Context) { path := ctx.Params("*") isHead := ctx.Req.Method == "HEAD" isGet := ctx.Req.Method == "GET" diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 9510b17e2d..774eb948ac 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -634,8 +634,8 @@ func mustEnableAttachments(ctx *context.APIContext) { } // bind binding an obj to a func(ctx *context.APIContext) -func bind[T any](obj T) http.HandlerFunc { - return web.Wrap(func(ctx *context.APIContext) { +func bind[T any](_ T) any { + return func(ctx *context.APIContext) { theObj := new(T) // create a new form obj for every request but not use obj directly errs := binding.Bind(ctx.Req, theObj) if len(errs) > 0 { @@ -643,7 +643,7 @@ func bind[T any](obj T) http.HandlerFunc { return } web.SetForm(ctx, theObj) - }) + } } // The OAuth2 plugin is expected to be executed first, as it must ignore the user id stored diff --git a/routers/install/routes.go b/routers/install/routes.go index df82ba2e4c..f52539ec1b 100644 --- a/routers/install/routes.go +++ b/routers/install/routes.go @@ -81,14 +81,14 @@ func installRecovery(ctx goctx.Context) func(next http.Handler) http.Handler { } } -// Routes registers the install routes +// Routes registers the installation routes func Routes(ctx goctx.Context) *web.Route { r := web.NewRoute() for _, middle := range common.Middlewares() { r.Use(middle) } - r.Use(web.WrapWithPrefix("/assets/", public.AssetsHandlerFunc("/assets/"), "AssetsHandler")) + r.Use(web.MiddlewareWithPrefix("/assets/", nil, public.AssetsHandlerFunc("/assets/"))) r.Use(session.Sessioner(session.Options{ Provider: setting.SessionConfig.Provider, @@ -109,7 +109,7 @@ func Routes(ctx goctx.Context) *web.Route { r.Get("/post-install", InstallDone) r.Get("/api/healthz", healthcheck.Check) - r.NotFound(web.Wrap(installNotFound)) + r.NotFound(installNotFound) return r } diff --git a/routers/private/internal.go b/routers/private/internal.go index b4d32c37a6..c6ea9e0263 100644 --- a/routers/private/internal.go +++ b/routers/private/internal.go @@ -38,12 +38,12 @@ func CheckInternalToken(next http.Handler) http.Handler { } // bind binding an obj to a handler -func bind[T any](obj T) http.HandlerFunc { - return web.Wrap(func(ctx *context.PrivateContext) { +func bind[T any](_ T) any { + return func(ctx *context.PrivateContext) { theObj := new(T) // create a new form obj for every request but not use obj directly binding.Bind(ctx.Req, theObj) web.SetForm(ctx, theObj) - }) + } } // Routes registers all internal APIs routes to web application. diff --git a/routers/web/web.go b/routers/web/web.go index 8f0b0b1f45..fc484eed4c 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -103,7 +103,7 @@ func buildAuthGroup() *auth_service.Group { func Routes(ctx gocontext.Context) *web.Route { routes := web.NewRoute() - routes.Use(web.WrapWithPrefix("/assets/", web.Wrap(CorsHandler(), public.AssetsHandlerFunc("/assets/")), "AssetsHandler")) + routes.Use(web.MiddlewareWithPrefix("/assets/", CorsHandler(), public.AssetsHandlerFunc("/assets/"))) sessioner := session.Sessioner(session.Options{ Provider: setting.SessionConfig.Provider, @@ -123,8 +123,8 @@ func Routes(ctx gocontext.Context) *web.Route { routes.Use(Recovery(ctx)) // We use r.Route here over r.Use because this prevents requests that are not for avatars having to go through this additional handler - routes.Route("/avatars/*", "GET, HEAD", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars)) - routes.Route("/repo-avatars/*", "GET, HEAD", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars)) + routes.RouteMethods("/avatars/*", "GET, HEAD", storageHandler(setting.Avatar.Storage, "avatars", storage.Avatars)) + routes.RouteMethods("/repo-avatars/*", "GET, HEAD", storageHandler(setting.RepoAvatar.Storage, "repo-avatars", storage.RepoAvatars)) // for health check - doesn't need to be passed through gzip handler routes.Head("/", func(w http.ResponseWriter, req *http.Request) { @@ -153,7 +153,7 @@ func Routes(ctx gocontext.Context) *web.Route { if setting.Service.EnableCaptcha { // The captcha http.Handler should only fire on /captcha/* so we can just mount this on that url - routes.Route("/captcha/*", "GET,HEAD", append(common, captcha.Captchaer(context.GetImageCaptcha()))...) + routes.RouteMethods("/captcha/*", "GET,HEAD", append(common, captcha.Captchaer(context.GetImageCaptcha()))...) } if setting.HasRobotsTxt { @@ -856,7 +856,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/delete", org.SecretsDelete) }) - m.Route("/delete", "GET,POST", org.SettingsDelete) + m.RouteMethods("/delete", "GET,POST", org.SettingsDelete) m.Group("/packages", func() { m.Get("", org.Packages) |