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

Add Find to Routes interface #872

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

Conversation

joeriddles
Copy link

@joeriddles joeriddles commented Nov 2, 2023

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 the Routes interface. This Find method closely mirrors the Match function that already exists. Unlike Match, 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 for Match. I refactored Match to use Find 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.

@Pipello
Copy link

Pipello commented Nov 5, 2023

That's cool feature 💯

@pkieltyka
Copy link
Member

hi @joeriddles what will be the official middleware repo for your package?

_examples/api-middleware/main.go Outdated Show resolved Hide resolved
_examples/api-middleware/main.go Outdated Show resolved Hide resolved
_examples/api-middleware/main.go Outdated Show resolved Hide resolved
@joeriddles
Copy link
Author

Thanks for the review @pkieltyka! I have renamed the file and variables you requested in this commit: 5cf9bc3

hi @joeriddles what will be the official middleware repo for your package?

Do you mean I should add a new middleware file, like middlewares/find.go? I can add something like this that allows the user to pass a function that should be called with the found route pattern.

@joeriddles
Copy link
Author

@pkieltyka I have added a new middleware here: f6c1df8. I also updated the example I added to use the new FindPattern middleware. Thanks again for reviewing!

@casoetan
Copy link

👀 🙌🏾

@batazor
Copy link

batazor commented Apr 20, 2024

any news?

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

Successfully merging this pull request may close these issues.

None yet

5 participants