-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from 2 commits
84a107d
e9f9213
776b548
a976152
e4c8c20
7441813
fc0f840
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,10 +13,12 @@ import ( | |||||
// directly means we'd get a collision with any other package that does the same. | ||||||
// https://play.golang.org/p/MxhRiL37R-9 | ||||||
type routerContextKeyType struct{} | ||||||
type routerPathContextKeyType struct{} | ||||||
|
||||||
var ( | ||||||
routerContextKey = routerContextKeyType{} | ||||||
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`) | ||||||
routerContextKey = routerContextKeyType{} | ||||||
routerPathContextKey = routerPathContextKeyType{} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Optional: This might be clearer to distinguish between path and pattern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed in 776b548 👍 |
||||||
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`) | ||||||
) | ||||||
|
||||||
type routerEntry struct { | ||||||
|
@@ -44,6 +46,13 @@ func RouterForRequest(r Request) *Router { | |||||
return nil | ||||||
} | ||||||
|
||||||
func routerEntryPathPatternForRequest(r Request) string { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed in fc0f840 👍 |
||||||
if v := r.Context.Value(routerPathContextKey); v != nil { | ||||||
return v.(string) | ||||||
} | ||||||
return "" | ||||||
} | ||||||
|
||||||
func (r *Router) compile(pattern string) *regexp.Regexp { | ||||||
re, pos := ``, 0 | ||||||
for _, m := range routerComponentsRe.FindAllStringSubmatchIndex(pattern, -1) { | ||||||
|
@@ -116,14 +125,15 @@ func (r Router) Lookup(method, path string) (Service, string, map[string]string, | |||||
// Serve returns a Service which will route inbound requests to the enclosed routes. | ||||||
func (r Router) Serve() Service { | ||||||
return func(req Request) Response { | ||||||
svc, _, ok := r.lookup(req.Method, req.URL.Path, nil) | ||||||
svc, pathPattern, ok := r.lookup(req.Method, req.URL.Path, nil) | ||||||
if !ok { | ||||||
txt := fmt.Sprintf("No handler for %s %s", req.Method, req.URL.Path) | ||||||
rsp := NewResponse(req) | ||||||
rsp.Error = terrors.NotFound("no_handler", txt, nil) | ||||||
return rsp | ||||||
} | ||||||
req.Context = context.WithValue(req.Context, routerContextKey, &r) | ||||||
req.Context = context.WithValue(req.Context, routerPathContextKey, pathPattern) | ||||||
rsp := svc(req) | ||||||
if rsp.Request == nil { | ||||||
rsp.Request = &req | ||||||
|
@@ -147,37 +157,46 @@ func (r Router) Params(req Request) map[string]string { | |||||
// Sugar | ||||||
|
||||||
// GET is shorthand for: | ||||||
// r.Register("GET", pattern, svc) | ||||||
// | ||||||
// r.Register("GET", pattern, svc) | ||||||
func (r *Router) GET(pattern string, svc Service) { r.Register("GET", pattern, svc) } | ||||||
|
||||||
// CONNECT is shorthand for: | ||||||
// r.Register("CONNECT", pattern, svc) | ||||||
// | ||||||
// r.Register("CONNECT", pattern, svc) | ||||||
func (r *Router) CONNECT(pattern string, svc Service) { r.Register("CONNECT", pattern, svc) } | ||||||
|
||||||
// DELETE is shorthand for: | ||||||
// r.Register("DELETE", pattern, svc) | ||||||
// | ||||||
// r.Register("DELETE", pattern, svc) | ||||||
func (r *Router) DELETE(pattern string, svc Service) { r.Register("DELETE", pattern, svc) } | ||||||
|
||||||
// HEAD is shorthand for: | ||||||
// r.Register("HEAD", pattern, svc) | ||||||
// | ||||||
// r.Register("HEAD", pattern, svc) | ||||||
func (r *Router) HEAD(pattern string, svc Service) { r.Register("HEAD", pattern, svc) } | ||||||
|
||||||
// OPTIONS is shorthand for: | ||||||
// r.Register("OPTIONS", pattern, svc) | ||||||
// | ||||||
// r.Register("OPTIONS", pattern, svc) | ||||||
func (r *Router) OPTIONS(pattern string, svc Service) { r.Register("OPTIONS", pattern, svc) } | ||||||
|
||||||
// PATCH is shorthand for: | ||||||
// r.Register("PATCH", pattern, svc) | ||||||
// | ||||||
// r.Register("PATCH", pattern, svc) | ||||||
func (r *Router) PATCH(pattern string, svc Service) { r.Register("PATCH", pattern, svc) } | ||||||
|
||||||
// POST is shorthand for: | ||||||
// r.Register("POST", pattern, svc) | ||||||
// | ||||||
// r.Register("POST", pattern, svc) | ||||||
func (r *Router) POST(pattern string, svc Service) { r.Register("POST", pattern, svc) } | ||||||
|
||||||
// PUT is shorthand for: | ||||||
// r.Register("PUT", pattern, svc) | ||||||
// | ||||||
// r.Register("PUT", pattern, svc) | ||||||
func (r *Router) PUT(pattern string, svc Service) { r.Register("PUT", pattern, svc) } | ||||||
|
||||||
// TRACE is shorthand for: | ||||||
// r.Register("TRACE", pattern, svc) | ||||||
// | ||||||
// r.Register("TRACE", pattern, svc) | ||||||
func (r *Router) TRACE(pattern string, svc Service) { r.Register("TRACE", pattern, svc) } |
There was a problem hiding this comment.
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...