@@ -13,10 +13,12 @@ import (
13
13
// directly means we'd get a collision with any other package that does the same.
14
14
// https://play.golang.org/p/MxhRiL37R-9
15
15
type routerContextKeyType struct {}
16
+ type routerRequestPatternContextKeyType struct {}
16
17
17
18
var (
18
- routerContextKey = routerContextKeyType {}
19
- routerComponentsRe = regexp .MustCompile (`(?:^|/)(\*\w*|:\w+)` )
19
+ routerContextKey = routerContextKeyType {}
20
+ routerRequestPatternContextKey = routerRequestPatternContextKeyType {}
21
+ routerComponentsRe = regexp .MustCompile (`(?:^|/)(\*\w*|:\w+)` )
20
22
)
21
23
22
24
type routerEntry struct {
@@ -44,6 +46,13 @@ func RouterForRequest(r Request) *Router {
44
46
return nil
45
47
}
46
48
49
+ func routerPathPatternForRequest (r Request ) string {
50
+ if v := r .Context .Value (routerRequestPatternContextKey ); v != nil {
51
+ return v .(string )
52
+ }
53
+ return ""
54
+ }
55
+
47
56
func (r * Router ) compile (pattern string ) * regexp.Regexp {
48
57
re , pos := `` , 0
49
58
for _ , m := range routerComponentsRe .FindAllStringSubmatchIndex (pattern , - 1 ) {
@@ -116,14 +125,15 @@ func (r Router) Lookup(method, path string) (Service, string, map[string]string,
116
125
// Serve returns a Service which will route inbound requests to the enclosed routes.
117
126
func (r Router ) Serve () Service {
118
127
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 )
120
129
if ! ok {
121
130
txt := fmt .Sprintf ("No handler for %s %s" , req .Method , req .URL .Path )
122
131
rsp := NewResponse (req )
123
132
rsp .Error = terrors .NotFound ("no_handler" , txt , nil )
124
133
return rsp
125
134
}
126
135
req .Context = context .WithValue (req .Context , routerContextKey , & r )
136
+ req .Context = context .WithValue (req .Context , routerRequestPatternContextKey , pathPattern )
127
137
rsp := svc (req )
128
138
if rsp .Request == nil {
129
139
rsp .Request = & req
0 commit comments