From bf817a14c7effbebb1b2fec6a6f47f7a2593b858 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Thu, 13 Nov 2025 14:53:46 -0300 Subject: [PATCH] feat(router): use new smalltrip APIs --- router/router.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/router/router.go b/router/router.go index bb1047a..32c1a6b 100644 --- a/router/router.go +++ b/router/router.go @@ -5,7 +5,6 @@ import ( "io/fs" "log/slog" "net/http" - "strings" "code.capytal.cc/capytal/comicverse/service" "code.capytal.cc/capytal/comicverse/templates" @@ -89,8 +88,16 @@ func (router *router) setup() http.Handler { log.Debug("Initializing router") + mux := multiplexer.New() + mux = multiplexer.WithFormMethod(mux, "x-method") + mux = multiplexer.WithPatternRules(mux, + multiplexer.EnsureMethod(), + multiplexer.EnsureStrictEnd(), + multiplexer.EnsureTrailingSlash(), + ) + r := smalltrip.NewRouter( - smalltrip.WithAssertions(router.assert), + smalltrip.WithMultiplexer(mux), smalltrip.WithLogger(log.WithGroup("smalltrip")), ) @@ -116,11 +123,11 @@ func (router *router) setup() http.Handler { }) projectController := newProjectController(router.projectService, router.templates, router.assert) - r.Handle("/assets/", http.StripPrefix("/assets/", http.FileServerFS(router.assets))) + r.Handle("GET /assets/{_file...}", http.StripPrefix("/assets/", http.FileServerFS(router.assets))) r.Use(userController.userMiddleware) - r.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) { + r.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { // TODO: Add a way to the user to bypass this check and see the landing page. // Probably a query parameter to bypass like "?landing=true" if _, ok := NewUserContext(r.Context()).GetUserID(); ok { @@ -134,8 +141,10 @@ func (router *router) setup() http.Handler { } }) - r.HandleFunc("/login/{$}", userController.login) - r.HandleFunc("/register/{$}", userController.register) + r.HandleFunc("GET /login/{$}", userController.login) + r.HandleFunc("POST /login/{$}", userController.login) + r.HandleFunc("GET /register/{$}", userController.register) + r.HandleFunc("POST /register/{$}", userController.register) // TODO: Provide/redirect short project-id paths to long paths with the project title as URL /projects/title-of-the-project- r.HandleFunc("GET /p/{projectID}/{$}", projectController.getProject) @@ -143,14 +152,3 @@ func (router *router) setup() http.Handler { return r } - -// getMethod is a helper function to get the HTTP method of request, tacking precedence -// the "x-method" argument sent by requests via form or query values. -func getMethod(r *http.Request) string { - m := r.FormValue("x-method") - if m != "" { - return strings.ToUpper(m) - } - - return strings.ToUpper(r.Method) -}