gorilla / mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
20.92k stars 1.85k forks source link

Feature router in request context #761

Closed cornejong closed 5 months ago

cornejong commented 5 months ago

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

Description

Named routes are great, and being able to retrieve the URL for a route based on its name is a feature I use a lot. However, when handlers reside in a different package from the router instance, this can become cumbersome. Therefore, it would be great if we could fetch the current router just like we can fetch the current route. This PR adds that functionality.

router := mux.CurrentRouter(r)

By default, the router is added to the request context but can be omitted using the omitRouterFromContext option or by calling router.OmitRouterFromContext(true).

Related Tickets & Documents

Added/updated tests?

Run verifications and test

AlexVulaj commented 5 months ago

I really like this change - I foresee some good use cases in middleware or anything that handles multiple routes. Thanks for the contribution!

codecov[bot] commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 81.35%. Comparing base (7d9a6f7) to head (40d0f3c). Report is 7 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #761 +/- ## ========================================== + Coverage 79.58% 81.35% +1.76% ========================================== Files 5 5 Lines 911 976 +65 ========================================== + Hits 725 794 +69 + Misses 131 129 -2 + Partials 55 53 -2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

cornejong commented 5 months ago

Thanks for the merge! Always happy to contribute.