-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: error custom edge case #379
base: main
Are you sure you want to change the base?
Conversation
gm @ccoVeille kindly review :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good PR, well structured!
Please add tests to show that it works for several patterns, and inform our users of this behaviour in the docs :)
// 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) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not re-define this middleware each time the option is called. Place the middleware elsewhere :) Also, I think the middleware should be placed in Server.globalMiddlewares
, see the difference in the docs.
// 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()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good! With one Server Option, they will be able to have this feature :)
I think it should look like this:
s.routeOptions = append(s.routeOptions, OptionStripTrailingSlash())
s.globalMiddlewares = append(s.globalMiddlewares, stripTrailingSlashesMiddleware)
closes #322