-
Notifications
You must be signed in to change notification settings - Fork 8
/
hitch.go
103 lines (85 loc) · 3.21 KB
/
hitch.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
package hitch
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
// Middleware wraps an http.Handler, returning a new http.Handler.
type Middleware func(http.Handler) http.Handler
// Hitch ties httprouter, context, and middleware up in a bow.
type Hitch struct {
Router *httprouter.Router
middleware []Middleware
}
// New initializes a new Hitch.
func New() *Hitch {
r := httprouter.New()
r.HandleMethodNotAllowed = false // may cause problems otherwise
return &Hitch{
Router: r,
}
}
// Use installs one or more middleware in the Hitch request cycle.
func (h *Hitch) Use(middleware ...Middleware) {
h.middleware = append(h.middleware, middleware...)
}
// UseHandler registers an http.Handler as a middleware.
func (h *Hitch) UseHandler(handler http.Handler) {
h.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
handler.ServeHTTP(w, req)
next.ServeHTTP(w, req)
})
})
}
// Next registers an http.Handler as a fallback/not-found handler.
func (h *Hitch) Next(handler http.Handler) {
h.Router.NotFound = handler
}
// Handle registers a handler for the given method and path.
func (h *Hitch) Handle(method, path string, handler http.Handler, middleware ...Middleware) {
for i := len(middleware) - 1; i >= 0; i-- {
handler = middleware[i](handler)
}
h.Router.Handler(method, path, handler)
}
// HandleFunc registers a func handler for the given method and path.
func (h *Hitch) HandleFunc(method, path string, handler func(http.ResponseWriter, *http.Request), middleware ...Middleware) {
h.Handle(method, path, http.HandlerFunc(handler), middleware...)
}
// Get registers a GET handler for the given path.
func (h *Hitch) Get(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("GET", path, handler, middleware...)
}
// Put registers a PUT handler for the given path.
func (h *Hitch) Put(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("PUT", path, handler, middleware...)
}
// Post registers a POST handler for the given path.
func (h *Hitch) Post(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("POST", path, handler, middleware...)
}
// Patch registers a PATCH handler for the given path.
func (h *Hitch) Patch(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("PATCH", path, handler, middleware...)
}
// Delete registers a DELETE handler for the given path.
func (h *Hitch) Delete(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("DELETE", path, handler, middleware...)
}
// Options registers a OPTIONS handler for the given path.
func (h *Hitch) Options(path string, handler http.Handler, middleware ...Middleware) {
h.Handle("OPTIONS", path, handler, middleware...)
}
// Handler returns an http.Handler for the embedded router and middleware.
func (h *Hitch) Handler() http.Handler {
var handler http.Handler = h.Router
for i := len(h.middleware) - 1; i >= 0; i-- {
handler = h.middleware[i](handler)
}
return handler
}
// Params returns the httprouter.Params for req.
// This is just a passthrough to httprouter.ParamsFromContext.
func Params(req *http.Request) httprouter.Params {
return httprouter.ParamsFromContext(req.Context())
}