From d7427563b38313563d79159a4dfbf732b27b5e30 Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Sun, 2 Feb 2025 19:23:53 +0100 Subject: [PATCH] fix: updates --- README.md | 19 +++++++++++++++++++ server.go | 18 ++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9e10908f..403f400b 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ to develop APIs and web applications. `log/slog`, `context` and `html/template`. - **Routing**: Fuego router is based on Go 1.22 `net/http`, with grouping and middleware support +- Optional trailing slash handling: Configure whether `/users` and `/users/` should be treated as the same route - **Serialization/Deserialization**: Fuego automatically serializes and deserializes JSON, XML and HTML Forms based on user-provided structs (or not, if you want to do it yourself) @@ -67,6 +68,24 @@ to develop APIs and web applications. `templ` or `gomponents` - **Adaptors**: [Experimental] Fuego can be used with Gin. +## Configuration + +### Trailing Slashes + +By default, Fuego treats URLs with and without trailing slashes as distinct routes. You can configure the server to automatically strip trailing slashes using the `WithStripTrailingSlash` option: + +```go +s := fuego.NewServer( + fuego.WithStripTrailingSlash(), +) +``` + +When enabled: + +- `/api/users` and `/api/users/` will route to the same handler +- Routes registered with trailing slashes are automatically converted to non-trailing slash versions +- Improves URL consistency across your API + ## Examples ### Hello World diff --git a/server.go b/server.go index 182c4068..17f02150 100644 --- a/server.go +++ b/server.go @@ -450,14 +450,24 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) { } } -// WithStripTrailingSlash enables stripping of trailing slashes from routes and requests -// Default is false. +// WithStripTrailingSlash configures the server to automatically strip trailing slashes from URLs. +// When enabled, requests to paths ending with "/" will be treated the same as their non-trailing +// slash counterparts. For example, "/users/" and "/users" will route to the same handler. // -// Example: +// This option affects both route registration and incoming requests. When a request comes in with +// a trailing slash, it will be matched against the non-trailing slash route if one exists. + +// Example usage: // -// app := fuego.NewServer( +// s := fuego.NewServer( // fuego.WithStripTrailingSlash(), // ) + +// After enabling: +// - A route registered as "/api/users" will match both "/api/users" and "/api/users/" +// - A route registered as "/api/users/" will be automatically converted to "/api/users" +// +// Note: This option should be set during server initialization as it affects all routes. func WithStripTrailingSlash() func(*Server) { return func(s *Server) { // Add OptionStripTrailingSlash to the default route options