@@ -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 routerPathContextKeyType struct {}
16
17
17
18
var (
18
- routerContextKey = routerContextKeyType {}
19
- routerComponentsRe = regexp .MustCompile (`(?:^|/)(\*\w*|:\w+)` )
19
+ routerContextKey = routerContextKeyType {}
20
+ routerPathContextKey = routerPathContextKeyType {}
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 routerEntryPathPatternForRequest (r Request ) string {
50
+ if v := r .Context .Value (routerPathContextKey ); 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 , routerPathContextKey , pathPattern )
127
137
rsp := svc (req )
128
138
if rsp .Request == nil {
129
139
rsp .Request = & req
@@ -147,37 +157,46 @@ func (r Router) Params(req Request) map[string]string {
147
157
// Sugar
148
158
149
159
// GET is shorthand for:
150
- // r.Register("GET", pattern, svc)
160
+ //
161
+ // r.Register("GET", pattern, svc)
151
162
func (r * Router ) GET (pattern string , svc Service ) { r .Register ("GET" , pattern , svc ) }
152
163
153
164
// CONNECT is shorthand for:
154
- // r.Register("CONNECT", pattern, svc)
165
+ //
166
+ // r.Register("CONNECT", pattern, svc)
155
167
func (r * Router ) CONNECT (pattern string , svc Service ) { r .Register ("CONNECT" , pattern , svc ) }
156
168
157
169
// DELETE is shorthand for:
158
- // r.Register("DELETE", pattern, svc)
170
+ //
171
+ // r.Register("DELETE", pattern, svc)
159
172
func (r * Router ) DELETE (pattern string , svc Service ) { r .Register ("DELETE" , pattern , svc ) }
160
173
161
174
// HEAD is shorthand for:
162
- // r.Register("HEAD", pattern, svc)
175
+ //
176
+ // r.Register("HEAD", pattern, svc)
163
177
func (r * Router ) HEAD (pattern string , svc Service ) { r .Register ("HEAD" , pattern , svc ) }
164
178
165
179
// OPTIONS is shorthand for:
166
- // r.Register("OPTIONS", pattern, svc)
180
+ //
181
+ // r.Register("OPTIONS", pattern, svc)
167
182
func (r * Router ) OPTIONS (pattern string , svc Service ) { r .Register ("OPTIONS" , pattern , svc ) }
168
183
169
184
// PATCH is shorthand for:
170
- // r.Register("PATCH", pattern, svc)
185
+ //
186
+ // r.Register("PATCH", pattern, svc)
171
187
func (r * Router ) PATCH (pattern string , svc Service ) { r .Register ("PATCH" , pattern , svc ) }
172
188
173
189
// POST is shorthand for:
174
- // r.Register("POST", pattern, svc)
190
+ //
191
+ // r.Register("POST", pattern, svc)
175
192
func (r * Router ) POST (pattern string , svc Service ) { r .Register ("POST" , pattern , svc ) }
176
193
177
194
// PUT is shorthand for:
178
- // r.Register("PUT", pattern, svc)
195
+ //
196
+ // r.Register("PUT", pattern, svc)
179
197
func (r * Router ) PUT (pattern string , svc Service ) { r .Register ("PUT" , pattern , svc ) }
180
198
181
199
// TRACE is shorthand for:
182
- // r.Register("TRACE", pattern, svc)
200
+ //
201
+ // r.Register("TRACE", pattern, svc)
183
202
func (r * Router ) TRACE (pattern string , svc Service ) { r .Register ("TRACE" , pattern , svc ) }
0 commit comments