-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.go
48 lines (37 loc) · 1 KB
/
router.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
package main
import (
"net/http"
"github.com/gorilla/context"
"github.com/julienschmidt/httprouter"
)
type router struct {
*httprouter.Router
}
// NewRouter is a router constructor
func newRouter() *router {
return &router{httprouter.New()}
}
func (r *router) get(path string, handler http.Handler) {
r.GET(path, wrapHandler(handler))
}
func (r *router) post(path string, handler http.Handler) {
r.POST(path, wrapHandler(handler))
}
func (r *router) put(path string, handler http.Handler) {
r.PUT(path, wrapHandler(handler))
}
func (r *router) delete(path string, handler http.Handler) {
r.DELETE(path, wrapHandler(handler))
}
func (r *router) patch(path string, handler http.Handler) {
r.PATCH(path, wrapHandler(handler))
}
func (r *router) head(path string, handler http.Handler) {
r.HEAD(path, wrapHandler(handler))
}
func wrapHandler(h http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
context.Set(r, "params", ps)
h.ServeHTTP(w, r)
}
}