Closed cornejong closed 5 months ago
Currently, you can add middleware to a single route by wrapping the handler in it:
router.Handle("/", middleware(http.HandlerFunc(myHandlerFunc)))
This approach works fine for a single middleware but becomes cluttered quickly when adding more than one:
router.Handle("/", middleware1(middleware2(http.HandlerFunc(myHandlerFunc))))
This got me thinking that it would be great if we could assign middleware to a route using the .Use method, similar to how it's done on the router. This would result in cleaner, more consistent, and more readable code. For example:
router.HandleFunc("/", handlerFunc)
.Methods(http.MethodGet)
.Use(middleware1, middleware2)
.Name("route")
really appreciate creating this. I am needing this now to keep my sanity, lol.
would love to see this merged
This looks like a pretty solid addition, thanks for the contribution!
Thanks for the merge! Always happy to contribute.
What type of PR is this? (check all applicable)
Description
Added support for route specific middleware using the
.Use
method. Using the.Use
method onRoute
middlewares can be set on a specific route.Example:
Related Tickets & Documents
Added/updated tests?
Run verifications and test
make verify
is passingmake test
is passing