-
Notifications
You must be signed in to change notification settings - Fork 57
/
context.go
209 lines (174 loc) · 6.73 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//go:build go1.7
// +build go1.7
package httptreemux
import (
"context"
"net/http"
)
// ContextGroup is a wrapper around Group, with the purpose of mimicking its API, but with the use of http.HandlerFunc-based handlers.
// Instead of passing a parameter map via the handler (i.e. httptreemux.HandlerFunc), the path parameters are accessed via the request
// object's context.
type ContextGroup struct {
group *Group
}
// Use appends a middleware handler to the Group middleware stack.
func (cg *ContextGroup) Use(fn MiddlewareFunc) {
cg.group.Use(fn)
}
// UseHandler is like Use but accepts http.Handler middleware.
func (cg *ContextGroup) UseHandler(middleware func(http.Handler) http.Handler) {
cg.group.UseHandler(middleware)
}
// UsingContext wraps the receiver to return a new instance of a ContextGroup.
// The returned ContextGroup is a sibling to its wrapped Group, within the parent TreeMux.
// The choice of using a *Group as the receiver, as opposed to a function parameter, allows chaining
// while method calls between a TreeMux, Group, and ContextGroup. For example:
//
// tree := httptreemux.New()
// group := tree.NewGroup("/api")
//
// group.GET("/v1", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
// w.Write([]byte(`GET /api/v1`))
// })
//
// group.UsingContext().GET("/v2", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte(`GET /api/v2`))
// })
//
// http.ListenAndServe(":8080", tree)
func (g *Group) UsingContext() *ContextGroup {
return &ContextGroup{g}
}
// NewContextGroup adds a child context group to its path.
func (cg *ContextGroup) NewContextGroup(path string) *ContextGroup {
return &ContextGroup{cg.group.NewGroup(path)}
}
func (cg *ContextGroup) NewGroup(path string) *ContextGroup {
return cg.NewContextGroup(path)
}
func (cg *ContextGroup) wrapHandler(path string, handler HandlerFunc) HandlerFunc {
if len(cg.group.stack) > 0 {
handler = handlerWithMiddlewares(handler, cg.group.stack)
}
// add the context data after adding all middleware
fullPath := cg.group.path + path
return func(writer http.ResponseWriter, request *http.Request, m map[string]string) {
routeData := &contextData{
route: fullPath,
params: m,
}
request = request.WithContext(AddRouteDataToContext(request.Context(), routeData))
handler(writer, request, m)
}
}
// Handle allows handling HTTP requests via an http.HandlerFunc, as opposed to an httptreemux.HandlerFunc.
// Any parameters from the request URL are stored in a map[string]string in the request's context.
func (cg *ContextGroup) Handle(method, path string, handler http.HandlerFunc) {
cg.group.mux.mutex.Lock()
defer cg.group.mux.mutex.Unlock()
wrapped := cg.wrapHandler(path, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
handler(w, r)
})
cg.group.addFullStackHandler(method, path, wrapped)
}
// Handler allows handling HTTP requests via an http.Handler interface, as opposed to an httptreemux.HandlerFunc.
// Any parameters from the request URL are stored in a map[string]string in the request's context.
func (cg *ContextGroup) Handler(method, path string, handler http.Handler) {
cg.group.mux.mutex.Lock()
defer cg.group.mux.mutex.Unlock()
wrapped := cg.wrapHandler(path, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
handler.ServeHTTP(w, r)
})
cg.group.addFullStackHandler(method, path, wrapped)
}
// GET is convenience method for handling GET requests on a context group.
func (cg *ContextGroup) GET(path string, handler http.HandlerFunc) {
cg.Handle("GET", path, handler)
}
// POST is convenience method for handling POST requests on a context group.
func (cg *ContextGroup) POST(path string, handler http.HandlerFunc) {
cg.Handle("POST", path, handler)
}
// PUT is convenience method for handling PUT requests on a context group.
func (cg *ContextGroup) PUT(path string, handler http.HandlerFunc) {
cg.Handle("PUT", path, handler)
}
// DELETE is convenience method for handling DELETE requests on a context group.
func (cg *ContextGroup) DELETE(path string, handler http.HandlerFunc) {
cg.Handle("DELETE", path, handler)
}
// PATCH is convenience method for handling PATCH requests on a context group.
func (cg *ContextGroup) PATCH(path string, handler http.HandlerFunc) {
cg.Handle("PATCH", path, handler)
}
// HEAD is convenience method for handling HEAD requests on a context group.
func (cg *ContextGroup) HEAD(path string, handler http.HandlerFunc) {
cg.Handle("HEAD", path, handler)
}
// OPTIONS is convenience method for handling OPTIONS requests on a context group.
func (cg *ContextGroup) OPTIONS(path string, handler http.HandlerFunc) {
cg.Handle("OPTIONS", path, handler)
}
type contextData struct {
route string
params map[string]string
}
func (cd *contextData) Route() string {
return cd.route
}
func (cd *contextData) Params() map[string]string {
if cd.params != nil {
return cd.params
}
return map[string]string{}
}
// ContextRouteData is the information associated with the matched path.
// Route() returns the matched route, without expanded wildcards.
// Params() returns a map of the route's wildcards and their matched values.
type ContextRouteData interface {
Route() string
Params() map[string]string
}
// ContextParams returns a map of the route's wildcards and their matched values.
func ContextParams(ctx context.Context) map[string]string {
if cd := ContextData(ctx); cd != nil {
return cd.Params()
}
return map[string]string{}
}
// ContextRoute returns the matched route, without expanded wildcards.
func ContextRoute(ctx context.Context) string {
if cd := ContextData(ctx); cd != nil {
return cd.Route()
}
return ""
}
// ContextData returns the ContextRouteData associated with the matched path
func ContextData(ctx context.Context) ContextRouteData {
if p, ok := ctx.Value(contextDataKey).(ContextRouteData); ok {
return p
}
return nil
}
// AddRouteDataToContext can be used for testing handlers, to insert route data into the request's `Context`.
func AddRouteDataToContext(ctx context.Context, data ContextRouteData) context.Context {
return context.WithValue(ctx, contextDataKey, data)
}
// AddParamsToContext inserts a parameters map into a context using
// the package's internal context key.
func AddParamsToContext(ctx context.Context, params map[string]string) context.Context {
return AddRouteDataToContext(ctx, &contextData{
params: params,
})
}
// AddRouteToContext inserts a route into a context using
// the package's internal context key.
func AddRouteToContext(ctx context.Context, route string) context.Context {
return AddRouteDataToContext(ctx, &contextData{
route: route,
})
}
type contextKey int
// contextDataKey is used to retrieve the path's params map and matched route
// from a request's context.
const contextDataKey contextKey = 0