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 router in request context #761

Merged
merged 3 commits into from
Jun 19, 2024

Conversation

cornejong
Copy link

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

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

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

  • Related Issue #
  • Closes #

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

@AlexVulaj
Copy link
Member

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

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@AlexVulaj AlexVulaj force-pushed the feature-router-in-request-context branch from 2f46728 to 40d0f3c Compare June 19, 2024 23:47
@AlexVulaj AlexVulaj enabled auto-merge (rebase) June 19, 2024 23:49
@AlexVulaj AlexVulaj merged commit db9d1d0 into gorilla:main Jun 19, 2024
8 of 12 checks passed
@cornejong cornejong deleted the feature-router-in-request-context branch June 20, 2024 17:16
@cornejong
Copy link
Author

Thanks for the merge! Always happy to contribute.

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.

2 participants