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
Add Find
to Routes
interface
#872
base: master
Are you sure you want to change the base?
Conversation
That's cool feature 💯 |
hi @joeriddles what will be the official middleware repo for your package? |
Thanks for the review @pkieltyka! I have renamed the file and variables you requested in this commit: 5cf9bc3
Do you mean I should add a new middleware file, like |
@pkieltyka I have added a new middleware here: f6c1df8. I also updated the example I added to use the new |
👀 🙌🏾 |
any news? |
Background
I am working on adding a middleware for OpenTelemetry to a Chi-based service that needs to send a metric before the request has executed. We need to include a string for the API endpoint, like the route pattern, in the metric. This is currently not possible due to the way request route patterns are resolved using Chi's trie-based router, as noted by the maintainer in this issue #692 (comment).
Description
This PR proposes adding a
Find
function to theRoutes
interface. ThisFind
method closely mirrors theMatch
function that already exists. UnlikeMatch
, which returns a boolean value if a route exists for the path/HTTP method combination,Find
returns the pattern that was matched.Unit tests have been added for
Find
, as well as new tests added forMatch
. I refactoredMatch
to useFind
under the hood, since they are both doing almost the same thing.This PR also adds a new middleware,
FindPattern
, which allows the user to pass a callback function that will receive the fully resolved route pattern from the HTTP request.Examples
A basic example of using
Find
in a middleware before the request is handled can be found here: https://github.com/joeriddles/chi-api-middleware, specifically this function https://github.com/joeriddles/chi-api-middleware/blob/main/main.go#L37.A smaller example has also been added to the
_examples
folder.