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 RequestPathPattern method to Typhon Request #169

Merged
merged 7 commits into from
Dec 4, 2023

Conversation

RohanPadmanabhan
Copy link
Contributor

@RohanPadmanabhan RohanPadmanabhan commented Dec 1, 2023

📚 Background

We have found several use cases such as metrics, logging, and authorization where knowing the router path (without any dynamic parameters) is useful to avoid unbounded cardinality or make searching for a particular value easier. At the moment, it is doable to get this but it's not easy.

🆕 What does this PR do?

  1. Adds a RouterEndpointPattern method to get the router endpoint pattern
  2. Adds a RequestMethod method to get the request method. This is needed to fulfil an interface between the two APIs. I couldn't name it just Method as this clashes with the string inside the contained http.Request object

🛡️ Testing

Added new unittests.

return ""
}

// RequestMethod returns the HTTP method of the request
Copy link
Contributor Author

@RohanPadmanabhan RohanPadmanabhan Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not particularly keen on the name of this so any suggestions would be appreciated 🙇 . I'm also wondering if RouterEndpointMethod (which could be a "*") would be more beneficial here...

@RohanPadmanabhan RohanPadmanabhan changed the title Add router endpoint pattern method onto the Typhon Request Add router endpoint pattern method to Typhon Request Dec 1, 2023
request.go Outdated
@@ -213,6 +213,22 @@ func (r Request) ResponseWithCode(body interface{}, statusCode int) Response {
return rsp
}

// RouterEndpointPattern finds the router pattern that matches the request. This is only callable while the request
// is being served.
func (r Request) RouterEndpointPattern() string {
Copy link
Member

@priyeshpatel priyeshpatel Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestMethod() feels intuitive and simple given we can't use Method(). 👍

For this method, RouterEntryPattern() is probably the most correct, but would RequestPathPattern() or even RequestPattern() be simpler and consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to RequestPathPattern() in e9f9213. I suspect that RequestPattern by itself would require some background context to understand

request.go Outdated
Comment on lines 219 to 223
if router := RouterForRequest(r); router != nil {
if pathPattern := router.Pattern(r); pathPattern != "" {
return pathPattern
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative implementation here would be to store the pattern used to serve the request in the context, as we do with the router currently. Then we don't need to look up the pattern separately (which could theoretically have changed if a new route is registered in the intervening time).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to store instead in e9f9213 👍

router.go Outdated
routerContextKey = routerContextKeyType{}
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`)
routerContextKey = routerContextKeyType{}
routerPathContextKey = routerPathContextKeyType{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
routerPathContextKey = routerPathContextKeyType{}
routerRequestPatternContextKey = routerRequestPatternContextKeyType{}

Optional: This might be clearer to distinguish between path and pattern?

Copy link
Contributor Author

@RohanPadmanabhan RohanPadmanabhan Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in 776b548 👍

router.go Outdated
@@ -44,6 +46,13 @@ func RouterForRequest(r Request) *Router {
return nil
}

func routerEntryPathPatternForRequest(r Request) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func routerEntryPathPatternForRequest(r Request) string {
func routerPathPatternForRequest(r Request) string {

Copy link
Contributor Author

@RohanPadmanabhan RohanPadmanabhan Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in fc0f840 👍

@RohanPadmanabhan RohanPadmanabhan marked this pull request as ready for review December 4, 2023 10:36
@RohanPadmanabhan RohanPadmanabhan merged commit ab6ccb7 into master Dec 4, 2023
2 checks passed
@RohanPadmanabhan RohanPadmanabhan deleted the add-router-endpoint-pattern branch December 4, 2023 10:47
@RohanPadmanabhan RohanPadmanabhan changed the title Add router endpoint pattern method to Typhon Request Add RequestPathPattern method to Typhon Request Dec 4, 2023
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.

2 participants