/
router.go
175 lines (147 loc) · 5.31 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
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
package http
import (
"net/http"
"path"
"sync"
)
// WalkRouteFunc is the type of the function called for each route visited by Walk.
type WalkRouteFunc func(RouteInfo) error
// RouteInfo is an HTTP route info.
type RouteInfo struct {
Path string
Method string
}
type Handler interface {
Prefix() string
Handle(router Router)
}
type (
// MiddlewareFunc defines a function to process middleware.
MiddlewareFunc func(next HandlerFunc) HandlerFunc
// HandlerFunc defines a function to serve HTTP requests.
HandlerFunc func(Context) error
BaseHandler struct{}
)
func (h *BaseHandler) Prefix() string {
return ""
}
func (h *BaseHandler) Handle(router Router) {}
type Router interface {
GET(path string, handler HandlerFunc, m ...MiddlewareFunc)
POST(path string, handler HandlerFunc, m ...MiddlewareFunc)
PUT(path string, handler HandlerFunc, m ...MiddlewareFunc)
DELETE(path string, handler HandlerFunc, m ...MiddlewareFunc)
HEAD(path string, handler HandlerFunc, m ...MiddlewareFunc)
PATCH(path string, handler HandlerFunc, m ...MiddlewareFunc)
CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc)
OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc)
TRACE(path string, h HandlerFunc, m ...MiddlewareFunc)
Group(prefix string, filters ...MiddlewareFunc) Router
}
// Router is an HTTP router.
type router struct {
prefix string
pool sync.Pool
srv *Server
preMiddleware []MiddlewareFunc
}
func newRouter(prefix string, srv *Server, middleware ...MiddlewareFunc) *router {
r := &router{
prefix: prefix,
srv: srv,
preMiddleware: middleware,
}
r.pool.New = func() interface{} {
return NewContext(r, nil, nil)
}
return r
}
// Handle registers a new route with a matcher for the URL path and method.
func (r *router) Handle(method, relativePath string, h HandlerFunc, middleware ...MiddlewareFunc) {
var next func(Context) error
if r.preMiddleware == nil {
next = applyMiddleware(h, middleware...)
} else {
next = applyMiddleware(h, middleware...)
next = applyMiddleware(next, r.preMiddleware...)
}
handler := http.Handler(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
ctx := r.pool.Get().(Context)
ctx.Reset(res, req)
if err := next(ctx); err != nil {
r.srv.ene(ctx, err)
}
ctx.Reset(nil, nil)
r.pool.Put(ctx)
}))
r.srv.router.Handle(path.Join(r.prefix, relativePath), handler).Methods(method)
}
// Group returns a new router group.
func (r *router) Group(prefix string, middleware ...MiddlewareFunc) Router {
var newMiddleware []MiddlewareFunc
newMiddleware = append(newMiddleware, r.preMiddleware...)
newMiddleware = append(newMiddleware, middleware...)
return newRouter(path.Join(r.prefix, prefix), r.srv, newMiddleware...)
}
// GET registers a new GET route for a path with matching handler in the router.
func (r *router) GET(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodGet, path, h, m...)
}
// HEAD registers a new HEAD route for a path with matching handler in the router.
func (r *router) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodHead, path, h, m...)
}
// POST registers a new POST route for a path with matching handler in the router.
func (r *router) POST(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodPost, path, h, m...)
}
// PUT registers a new PUT route for a path with matching handler in the router.
func (r *router) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodPut, path, h, m...)
}
// PATCH registers a new PATCH route for a path with matching handler in the router.
func (r *router) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodPatch, path, h, m...)
}
// DELETE registers a new DELETE route for a path with matching handler in the router.
func (r *router) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodDelete, path, h, m...)
}
// CONNECT registers a new CONNECT route for a path with matching handler in the router.
func (r *router) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodConnect, path, h, m...)
}
// OPTIONS registers a new OPTIONS route for a path with matching handler in the router.
func (r *router) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodOptions, path, h, m...)
}
// TRACE registers a new TRACE route for a path with matching handler in the router.
func (r *router) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) {
r.Handle(http.MethodTrace, path, h, m...)
}
func applyMiddleware(h HandlerFunc, middleware ...MiddlewareFunc) HandlerFunc {
for i := len(middleware) - 1; i >= 0; i-- {
h = middleware[i](h)
}
return h
}
// WrapHandler wraps `http.Handler` into `http.HandlerFunc`.
func WrapHandler(h http.Handler) HandlerFunc {
return func(c Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}
// WrapMiddleware wraps `func(http.Handler) http.Handler` into `http.MiddlewareFunc`
func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
return func(next HandlerFunc) HandlerFunc {
return func(c Context) (err error) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.SetRequest(r)
c.SetResponse(NewResponse(w))
err = next(c)
})).ServeHTTP(c.Response(), c.Request())
return
}
}
}