Skip to content

Commit bb1576e

Browse files
Rename function and store path pattern inside context
1 parent 84a107d commit bb1576e

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

request.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ func (r Request) ResponseWithCode(body interface{}, statusCode int) Response {
213213
return rsp
214214
}
215215

216-
// RouterEndpointPattern finds the router pattern that matches the request. This is only callable while the request
217-
// is being served.
218-
func (r Request) RouterEndpointPattern() string {
216+
// RequestPathPattern finds the router entry pattern that matches the request
217+
func (r Request) RequestPathPattern() string {
219218
if router := RouterForRequest(r); router != nil {
220219
if pathPattern := router.Pattern(r); pathPattern != "" {
221220
return pathPattern

request_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ func TestRequestSetMetadata(t *testing.T) {
226226

227227
func TestRouterEndpointPattern(t *testing.T) {
228228
req := NewRequest(context.Background(), http.MethodGet, "/foo/some-url-identifier", nil)
229-
assert.Equal(t, "", req.RouterEndpointPattern()) // should be empty if request has not been served by a router
229+
assert.Equal(t, "", req.RequestPathPattern()) // should be empty if request has not been served by a router
230230

231231
router := Router{}
232232
routerEndpointPattern := "/foo/:id"
233233
router.GET(routerEndpointPattern, func(req Request) Response {
234234
// as we are currently serving the request, we should be able to get the router endpoint pattern
235-
assert.Equal(t, routerEndpointPattern, req.RouterEndpointPattern())
235+
assert.Equal(t, routerEndpointPattern, req.RequestPathPattern())
236236
return req.Response(nil)
237237
})
238238

router.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
// directly means we'd get a collision with any other package that does the same.
1414
// https://play.golang.org/p/MxhRiL37R-9
1515
type routerContextKeyType struct{}
16+
type routerPathContextKeyType struct{}
1617

1718
var (
18-
routerContextKey = routerContextKeyType{}
19-
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`)
19+
routerContextKey = routerContextKeyType{}
20+
routerPathContextKey = routerPathContextKeyType{}
21+
routerComponentsRe = regexp.MustCompile(`(?:^|/)(\*\w*|:\w+)`)
2022
)
2123

2224
type routerEntry struct {
@@ -44,6 +46,13 @@ func RouterForRequest(r Request) *Router {
4446
return nil
4547
}
4648

49+
func routerEntryPathPatternForRequest(r Request) string {
50+
if v := r.Context.Value(routerPathContextKey); v != nil {
51+
return v.(string)
52+
}
53+
return ""
54+
}
55+
4756
func (r *Router) compile(pattern string) *regexp.Regexp {
4857
re, pos := ``, 0
4958
for _, m := range routerComponentsRe.FindAllStringSubmatchIndex(pattern, -1) {
@@ -116,14 +125,15 @@ func (r Router) Lookup(method, path string) (Service, string, map[string]string,
116125
// Serve returns a Service which will route inbound requests to the enclosed routes.
117126
func (r Router) Serve() Service {
118127
return func(req Request) Response {
119-
svc, _, ok := r.lookup(req.Method, req.URL.Path, nil)
128+
svc, pathPattern, ok := r.lookup(req.Method, req.URL.Path, nil)
120129
if !ok {
121130
txt := fmt.Sprintf("No handler for %s %s", req.Method, req.URL.Path)
122131
rsp := NewResponse(req)
123132
rsp.Error = terrors.NotFound("no_handler", txt, nil)
124133
return rsp
125134
}
126135
req.Context = context.WithValue(req.Context, routerContextKey, &r)
136+
req.Context = context.WithValue(req.Context, routerPathContextKey, pathPattern)
127137
rsp := svc(req)
128138
if rsp.Request == nil {
129139
rsp.Request = &req
@@ -147,37 +157,46 @@ func (r Router) Params(req Request) map[string]string {
147157
// Sugar
148158

149159
// GET is shorthand for:
150-
// r.Register("GET", pattern, svc)
160+
//
161+
// r.Register("GET", pattern, svc)
151162
func (r *Router) GET(pattern string, svc Service) { r.Register("GET", pattern, svc) }
152163

153164
// CONNECT is shorthand for:
154-
// r.Register("CONNECT", pattern, svc)
165+
//
166+
// r.Register("CONNECT", pattern, svc)
155167
func (r *Router) CONNECT(pattern string, svc Service) { r.Register("CONNECT", pattern, svc) }
156168

157169
// DELETE is shorthand for:
158-
// r.Register("DELETE", pattern, svc)
170+
//
171+
// r.Register("DELETE", pattern, svc)
159172
func (r *Router) DELETE(pattern string, svc Service) { r.Register("DELETE", pattern, svc) }
160173

161174
// HEAD is shorthand for:
162-
// r.Register("HEAD", pattern, svc)
175+
//
176+
// r.Register("HEAD", pattern, svc)
163177
func (r *Router) HEAD(pattern string, svc Service) { r.Register("HEAD", pattern, svc) }
164178

165179
// OPTIONS is shorthand for:
166-
// r.Register("OPTIONS", pattern, svc)
180+
//
181+
// r.Register("OPTIONS", pattern, svc)
167182
func (r *Router) OPTIONS(pattern string, svc Service) { r.Register("OPTIONS", pattern, svc) }
168183

169184
// PATCH is shorthand for:
170-
// r.Register("PATCH", pattern, svc)
185+
//
186+
// r.Register("PATCH", pattern, svc)
171187
func (r *Router) PATCH(pattern string, svc Service) { r.Register("PATCH", pattern, svc) }
172188

173189
// POST is shorthand for:
174-
// r.Register("POST", pattern, svc)
190+
//
191+
// r.Register("POST", pattern, svc)
175192
func (r *Router) POST(pattern string, svc Service) { r.Register("POST", pattern, svc) }
176193

177194
// PUT is shorthand for:
178-
// r.Register("PUT", pattern, svc)
195+
//
196+
// r.Register("PUT", pattern, svc)
179197
func (r *Router) PUT(pattern string, svc Service) { r.Register("PUT", pattern, svc) }
180198

181199
// TRACE is shorthand for:
182-
// r.Register("TRACE", pattern, svc)
200+
//
201+
// r.Register("TRACE", pattern, svc)
183202
func (r *Router) TRACE(pattern string, svc Service) { r.Register("TRACE", pattern, svc) }

0 commit comments

Comments
 (0)