Skip to content

Commit

Permalink
fix: error custom edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvibes committed Jan 31, 2025
1 parent 24cbc28 commit 6d889ac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -438,3 +439,29 @@ func OptionSecurity(securityRequirements ...openapi3.SecurityRequirement) func(*
*r.Operation.Security = append(*r.Operation.Security, securityRequirements...)
}
}

// OptionStripTrailingSlash removes trailing slashes from both the route path and incoming requests.
// This can be applied globally using WithStripTrailingSlash() or per-route.
// For example: "/users/" becomes "/users" for both route definition and request handling.
func OptionStripTrailingSlash() func(*BaseRoute) {
return func(r *BaseRoute) {
// Strip trailing slash from route path
if len(r.Path) > 1 {
r.Path = strings.TrimRight(r.Path, "/")
}

// Add middleware to strip trailing slash from requests
stripSlashMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path) > 1 {
r.URL.Path = strings.TrimRight(r.URL.Path, "/")
}
next.ServeHTTP(w, r)
})
}

// Add the middleware to the route
r.Middlewares = append(r.Middlewares, stripSlashMiddleware)
}
}

Check failure on line 467 in option.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gci)
15 changes: 15 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,18 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) {
}
}
}

// WithStripTrailingSlash enables stripping of trailing slashes from routes and requests
// Default is false.
//
// Example:
//
// app := fuego.NewServer(
// fuego.WithStripTrailingSlash(),
// )
func WithStripTrailingSlash() func(*Server) {
return func(s *Server) {
// Add OptionStripTrailingSlash to the default route options
s.routeOptions = append(s.routeOptions, OptionStripTrailingSlash())
}
}

0 comments on commit 6d889ac

Please sign in to comment.