1
- package xhandler
2
-
3
- import (
4
- "net/http"
5
-
6
- "golang.org/x/net/context"
7
- )
8
-
9
- // Mux is a xhandler.HandlerC which can be used to dispatch requests to different
10
- // handler functions via configurable routes
1
+ // Package xmux is a net/context aware, tree based high performance HTTP request
2
+ // multiplexer forked from httprouter.
3
+ //
4
+ // A trivial example is:
5
+ //
6
+ // package main
7
+ //
8
+ // import (
9
+ // "fmt"
10
+ // "net/http"
11
+ // "log"
12
+ //
13
+ // "github.com/rs/xhandler"
14
+ // "github.com/rs/xhandler/xmux"
15
+ // "golang.org/x/net/context"
16
+ // )
17
+ //
18
+ // func Index(ctx context.Context, w http.ResponseWriter, r *http.Request) {
19
+ // fmt.Fprint(w, "Welcome!\n")
20
+ // }
21
+ //
22
+ // func Hello(ctx context.Context, w http.ResponseWriter, r *http.Request) {
23
+ // fmt.Fprintf(w, "hello, %s!\n", xmux.URLParams(ctx).Get("name"))
24
+ // }
25
+ //
26
+ // func main() {
27
+ // mux := xmux.New()
28
+ // mux.GET("/", Index)
29
+ // mux.GET("/hello/:name", Hello)
30
+ //
31
+ // log.Fatal(http.ListenAndServe(":8080", xhandler.New(context.Background(), mux)))
32
+ // }
11
33
//
12
34
// The muxer matches incoming requests by the request method and the path.
13
35
// If a handle is registered for this path and method, the router delegates the
@@ -45,6 +67,18 @@ import (
45
67
// The value of parameters is saved as aParams type saved into the context.
46
68
// Parameters can be retrieved by name using xhandler.URLParams(ctx).Get(name) method:
47
69
// user := xhandler.URLParams(ctx).Get("user") // defined by :user or *user
70
+ package xmux
71
+
72
+ import (
73
+ "net/http"
74
+
75
+ "github.com/rs/xhandler"
76
+
77
+ "golang.org/x/net/context"
78
+ )
79
+
80
+ // Mux is a xhandler.HandlerC which can be used to dispatch requests to different
81
+ // handler functions via configurable routes
48
82
type Mux struct {
49
83
trees map [string ]* node
50
84
@@ -76,12 +110,12 @@ type Mux struct {
76
110
77
111
// Configurable http.Handler which is called when no matching route is
78
112
// found. If it is not set, http.Error with http.StatusNotFound is used.
79
- NotFound HandlerC
113
+ NotFound xhandler. HandlerC
80
114
81
115
// Configurable http.Handler which is called when a request
82
116
// cannot be routed and HandleMethodNotAllowed is true.
83
117
// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
84
- MethodNotAllowed HandlerC
118
+ MethodNotAllowed xhandler. HandlerC
85
119
86
120
// Function to handle panics recovered from http handlers.
87
121
// It should be used to generate a error page and return the http error code
@@ -131,8 +165,8 @@ func URLParams(ctx context.Context) Params {
131
165
return emptyParams
132
166
}
133
167
134
- // NewMux returns a new muxer instance
135
- func NewMux () * Mux {
168
+ // New returns a new muxer instance
169
+ func New () * Mux {
136
170
return & Mux {
137
171
RedirectTrailingSlash : true ,
138
172
RedirectFixedPath : true ,
@@ -141,37 +175,37 @@ func NewMux() *Mux {
141
175
}
142
176
143
177
// GET is a shortcut for mux.Handle("GET", path, handler)
144
- func (mux * Mux ) GET (path string , handler HandlerC ) {
178
+ func (mux * Mux ) GET (path string , handler xhandler. HandlerC ) {
145
179
mux .Handle ("GET" , path , handler )
146
180
}
147
181
148
182
// HEAD is a shortcut for mux.Handle("HEAD", path, handler)
149
- func (mux * Mux ) HEAD (path string , handler HandlerC ) {
183
+ func (mux * Mux ) HEAD (path string , handler xhandler. HandlerC ) {
150
184
mux .Handle ("HEAD" , path , handler )
151
185
}
152
186
153
187
// OPTIONS is a shortcut for mux.Handle("OPTIONS", path, handler)
154
- func (mux * Mux ) OPTIONS (path string , handler HandlerC ) {
188
+ func (mux * Mux ) OPTIONS (path string , handler xhandler. HandlerC ) {
155
189
mux .Handle ("OPTIONS" , path , handler )
156
190
}
157
191
158
192
// POST is a shortcut for mux.Handle("POST", path, handler)
159
- func (mux * Mux ) POST (path string , handler HandlerC ) {
193
+ func (mux * Mux ) POST (path string , handler xhandler. HandlerC ) {
160
194
mux .Handle ("POST" , path , handler )
161
195
}
162
196
163
197
// PUT is a shortcut for mux.Handle("PUT", path, handler)
164
- func (mux * Mux ) PUT (path string , handler HandlerC ) {
198
+ func (mux * Mux ) PUT (path string , handler xhandler. HandlerC ) {
165
199
mux .Handle ("PUT" , path , handler )
166
200
}
167
201
168
202
// PATCH is a shortcut for mux.Handle("PATCH", path, handler)
169
- func (mux * Mux ) PATCH (path string , handler HandlerC ) {
203
+ func (mux * Mux ) PATCH (path string , handler xhandler. HandlerC ) {
170
204
mux .Handle ("PATCH" , path , handler )
171
205
}
172
206
173
207
// DELETE is a shortcut for mux.Handle("DELETE", path, handler)
174
- func (mux * Mux ) DELETE (path string , handler HandlerC ) {
208
+ func (mux * Mux ) DELETE (path string , handler xhandler. HandlerC ) {
175
209
mux .Handle ("DELETE" , path , handler )
176
210
}
177
211
@@ -183,7 +217,7 @@ func (mux *Mux) DELETE(path string, handler HandlerC) {
183
217
// This function is intended for bulk loading and to allow the usage of less
184
218
// frequently used, non-standardized or custom methods (e.g. for internal
185
219
// communication with a proxy).
186
- func (mux * Mux ) Handle (method , path string , handler HandlerC ) {
220
+ func (mux * Mux ) Handle (method , path string , handler xhandler. HandlerC ) {
187
221
if path [0 ] != '/' {
188
222
panic ("path must begin with '/' in path '" + path + "'" )
189
223
}
@@ -212,7 +246,7 @@ func (mux *Mux) recv(ctx context.Context, w http.ResponseWriter, r *http.Request
212
246
// If the path was found, it returns the handle function and the path parameter
213
247
// values. Otherwise the third return value indicates whether a redirection to
214
248
// the same path with an extra / without the trailing slash should be performed.
215
- func (mux * Mux ) Lookup (method , path string ) (HandlerC , Params , bool ) {
249
+ func (mux * Mux ) Lookup (method , path string ) (xhandler. HandlerC , Params , bool ) {
216
250
if root := mux .trees [method ]; root != nil {
217
251
return root .getValue (path )
218
252
}
0 commit comments