feat(router): use new smalltrip APIs

This commit is contained in:
Guz
2025-11-13 14:53:46 -03:00
parent 3e4380d9f8
commit bf817a14c7

View File

@@ -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-<start of uuid>
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)
}