@@ -14,10 +14,12 @@ import (
14
14
// https://play.golang.org/p/MxhRiL37R-9
15
15
type routerContextKeyType struct {}
16
16
type routerRequestPatternContextKeyType struct {}
17
+ type routerRequestMethodContextKeyType struct {}
17
18
18
19
var (
19
20
routerContextKey = routerContextKeyType {}
20
21
routerRequestPatternContextKey = routerRequestPatternContextKeyType {}
22
+ routerRequestMethodContextKey = routerRequestMethodContextKeyType {}
21
23
routerComponentsRe = regexp .MustCompile (`(?:^|/)(\*\w*|:\w+)` )
22
24
)
23
25
@@ -53,6 +55,22 @@ func routerPathPatternForRequest(r Request) string {
53
55
return ""
54
56
}
55
57
58
+ // RequestPatternFromContext returns the pattern that was matched for the request, if available.
59
+ func RequestPatternFromContext (ctx context.Context ) (string , bool ) {
60
+ if v := ctx .Value (routerRequestPatternContextKey ); v != nil {
61
+ return v .(string ), true
62
+ }
63
+ return "" , false
64
+ }
65
+
66
+ // RequestMethodFromContext returns the method of the request, if available.
67
+ func RequestMethodFromContext (ctx context.Context ) (string , bool ) {
68
+ if v := ctx .Value (routerRequestMethodContextKey ); v != nil {
69
+ return v .(string ), true
70
+ }
71
+ return "" , false
72
+ }
73
+
56
74
func (r * Router ) compile (pattern string ) * regexp.Regexp {
57
75
re , pos := `` , 0
58
76
for _ , m := range routerComponentsRe .FindAllStringSubmatchIndex (pattern , - 1 ) {
@@ -134,6 +152,7 @@ func (r Router) Serve() Service {
134
152
}
135
153
req .Context = context .WithValue (req .Context , routerContextKey , & r )
136
154
req .Context = context .WithValue (req .Context , routerRequestPatternContextKey , pathPattern )
155
+ req .Context = context .WithValue (req .Context , routerRequestMethodContextKey , req .Method )
137
156
rsp := svc (req )
138
157
if rsp .Request == nil {
139
158
rsp .Request = & req
@@ -157,37 +176,46 @@ func (r Router) Params(req Request) map[string]string {
157
176
// Sugar
158
177
159
178
// GET is shorthand for:
160
- // r.Register("GET", pattern, svc)
179
+ //
180
+ // r.Register("GET", pattern, svc)
161
181
func (r * Router ) GET (pattern string , svc Service ) { r .Register ("GET" , pattern , svc ) }
162
182
163
183
// CONNECT is shorthand for:
164
- // r.Register("CONNECT", pattern, svc)
184
+ //
185
+ // r.Register("CONNECT", pattern, svc)
165
186
func (r * Router ) CONNECT (pattern string , svc Service ) { r .Register ("CONNECT" , pattern , svc ) }
166
187
167
188
// DELETE is shorthand for:
168
- // r.Register("DELETE", pattern, svc)
189
+ //
190
+ // r.Register("DELETE", pattern, svc)
169
191
func (r * Router ) DELETE (pattern string , svc Service ) { r .Register ("DELETE" , pattern , svc ) }
170
192
171
193
// HEAD is shorthand for:
172
- // r.Register("HEAD", pattern, svc)
194
+ //
195
+ // r.Register("HEAD", pattern, svc)
173
196
func (r * Router ) HEAD (pattern string , svc Service ) { r .Register ("HEAD" , pattern , svc ) }
174
197
175
198
// OPTIONS is shorthand for:
176
- // r.Register("OPTIONS", pattern, svc)
199
+ //
200
+ // r.Register("OPTIONS", pattern, svc)
177
201
func (r * Router ) OPTIONS (pattern string , svc Service ) { r .Register ("OPTIONS" , pattern , svc ) }
178
202
179
203
// PATCH is shorthand for:
180
- // r.Register("PATCH", pattern, svc)
204
+ //
205
+ // r.Register("PATCH", pattern, svc)
181
206
func (r * Router ) PATCH (pattern string , svc Service ) { r .Register ("PATCH" , pattern , svc ) }
182
207
183
208
// POST is shorthand for:
184
- // r.Register("POST", pattern, svc)
209
+ //
210
+ // r.Register("POST", pattern, svc)
185
211
func (r * Router ) POST (pattern string , svc Service ) { r .Register ("POST" , pattern , svc ) }
186
212
187
213
// PUT is shorthand for:
188
- // r.Register("PUT", pattern, svc)
214
+ //
215
+ // r.Register("PUT", pattern, svc)
189
216
func (r * Router ) PUT (pattern string , svc Service ) { r .Register ("PUT" , pattern , svc ) }
190
217
191
218
// TRACE is shorthand for:
192
- // r.Register("TRACE", pattern, svc)
219
+ //
220
+ // r.Register("TRACE", pattern, svc)
193
221
func (r * Router ) TRACE (pattern string , svc Service ) { r .Register ("TRACE" , pattern , svc ) }
0 commit comments