From 14a2c044c0e672d1c35d39a4ce9a544691c3fc5b Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 12 Sep 2025 08:44:36 +0200 Subject: [PATCH] gateway: simplify mux configuration --- internal/services/gateway/gateway.go | 37 +++++++++++-------- internal/services/gateway/handlers/maxbyte.go | 10 +++-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index 183483d7..3681f6be 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -305,11 +305,6 @@ func (g *Gateway) Run(ctx context.Context) error { exportHandler := api.NewExportHandler(g.log, g.ah) importHandler := api.NewImportHandler(g.log, g.ah) - router := mux.NewRouter() - reposRouter := mux.NewRouter() - - apirouter := mux.NewRouter().PathPrefix("/api/v1alpha").Subrouter().UseEncodedPath() - authForcedHandler := func(h http.Handler) http.Handler { // first do auth, then check csrf (skipping it only on successful token auth) return handlers.NewAuthChecker(g.log, g.configstoreClient, handlers.WithTokenChecker(g.c.AdminToken), handlers.WithCookieChecker(g.sc, g.c.UnsecureCookies), handlers.WithRequired(true))(CSRF(h)) @@ -319,7 +314,23 @@ func (g *Gateway) Run(ctx context.Context) error { return handlers.NewAuthChecker(g.log, g.configstoreClient, handlers.WithTokenChecker(g.c.AdminToken), handlers.WithCookieChecker(g.sc, g.c.UnsecureCookies), handlers.WithRequired(false))(CSRF(h)) } - router.PathPrefix("/api/v1alpha").Handler(apirouter) + // router is the main router. It uses the recovery and cors middlewares + router := mux.NewRouter() + router.Use( + ghandlers.RecoveryHandler(ghandlers.PrintRecoveryStack(true)), + corsHandler, + ) + + // reposRouter doesn't use max bytes handler since git data can be quite big + reposRouter := router.PathPrefix("/repos/").Subrouter() + + // stdRouter additionally uses max bytes handler + stdRouter := router.PathPrefix("/").Subrouter() + stdRouter.Use( + handlers.NewMaxBytesHandler(maxRequestSize), + ) + + apirouter := stdRouter.PathPrefix("/api/v1alpha/").Subrouter().UseEncodedPath() //apirouter.Handle("/projectgroups", authForcedHandler(projectsHandler)).Methods("GET") apirouter.Handle("/projectgroups/{projectgroupref}", authForcedHandler(projectGroupHandler)).Methods("GET") @@ -426,16 +437,10 @@ func (g *Gateway) Run(ctx context.Context) error { apirouter.Handle("/import/{servicename}", authForcedHandler(importHandler)).Methods("POST") // TODO(sgotti) add auth to these requests - reposRouter.Handle("/repos/{rest:.*}", reposHandler).Methods("GET", "POST") + reposRouter.Handle("/{rest:.*}", reposHandler).Methods("GET", "POST") - router.Handle("/webhooks", webhooksHandler).Methods("POST") - router.PathPrefix("/").HandlerFunc(handlers.NewWebBundleHandlerFunc(g.c.APIExposedURL)) - - maxBytesHandler := handlers.NewMaxBytesHandler(router, maxRequestSize) - - mainrouter := mux.NewRouter() - mainrouter.PathPrefix("/repos/").Handler(corsHandler(reposRouter)) - mainrouter.PathPrefix("/").Handler(ghandlers.RecoveryHandler(ghandlers.PrintRecoveryStack(true))(corsHandler(maxBytesHandler))) + stdRouter.Handle("/webhooks", webhooksHandler).Methods("POST") + stdRouter.PathPrefix("/").HandlerFunc(handlers.NewWebBundleHandlerFunc(g.c.APIExposedURL)) var tlsConfig *tls.Config if g.c.Web.TLS { @@ -449,7 +454,7 @@ func (g *Gateway) Run(ctx context.Context) error { httpServer := http.Server{ Addr: g.c.Web.ListenAddress, - Handler: mainrouter, + Handler: router, TLSConfig: tlsConfig, } diff --git a/internal/services/gateway/handlers/maxbyte.go b/internal/services/gateway/handlers/maxbyte.go index 6e210833..a3c21172 100644 --- a/internal/services/gateway/handlers/maxbyte.go +++ b/internal/services/gateway/handlers/maxbyte.go @@ -21,10 +21,12 @@ type maxBytesHandler struct { n int64 } -func NewMaxBytesHandler(h http.Handler, n int64) *maxBytesHandler { - return &maxBytesHandler{ - h: h, - n: n, +func NewMaxBytesHandler(n int64) func(http.Handler) http.Handler { + return func(h http.Handler) http.Handler { + return &maxBytesHandler{ + h: h, + n: n, + } } }