Skip to content
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

Feature route middleware with .Use #759

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cornejong
Copy link

@cornejong cornejong commented May 15, 2024

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Go Version Update
  • Dependency Update

Description

Added support for route specific middleware using the .Use method.
Using the .Use method on Route middlewares can be set on a specific route.

Example:

router.HandleFunc("/", handlerFunc).Use(middleware)

Related Tickets & Documents

  • Related Issue #
  • Closes #

Added/updated tests?

  • Yes
  • No, and this is why:
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

@cornejong cornejong changed the title Feature route middleware Feature route middleware with .Use May 15, 2024
@cornejong
Copy link
Author

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")

@pcfreak30
Copy link

really appreciate creating this. I am needing this now to keep my sanity, lol.

would love to see this merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants