-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
225 lines (190 loc) · 6.89 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package lit
import (
"net/http"
"slices"
"sync"
"github.com/julienschmidt/httprouter"
)
// Handler handles requests.
type Handler func(r *Request) Response
// Base returns the equivalent http.HandlerFunc of this handler.
func (h Handler) Base() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
r := NewRequest(req)
if response := h(r); response != nil {
response.Write(w)
}
}
}
// Middleware transforms a Handler, extending its functionality.
type Middleware func(h Handler) Handler
// Router manages, listens and serves HTTP requests.
type Router struct {
router *httprouter.Router
requestPool sync.Pool
middlewares []Middleware
}
// NewRouter creates a new [Router] instance.
func NewRouter() *Router {
return &Router{
httprouter.New(),
sync.Pool{New: func() any { return NewEmptyRequest() }},
make([]Middleware, 0),
}
}
// Use registers m as a global middleware. They run in every request.
//
// Middlewares transform the request handler. They are applied in reverse order, and local middlewares
// are always applied first. For example, suppose there have been defined global middlewares G1 and G2 in this order and
// local middlewares L1 and L2 in this order. The response for the request r handled by h is
//
// (G1(G2(L1(L2(h)))))(r)
//
// Global middlewares should be set before any handler is registered.
//
// If m is nil, Use panics.
func (r *Router) Use(m Middleware) {
if m == nil {
panic("m should not be nil")
}
r.middlewares = append(r.middlewares, m)
}
// Handle registers handler for path and method and optional local middlewares.
//
// Middlewares transform handler. They are applied in reverse order, and local middlewares
// are always applied first. For example, suppose there have been defined global middlewares G1 and G2 in this order and
// local middlewares L1 and L2 in this order. The response for the request r is
//
// (G1(G2(L1(L2(handler)))))(r)
//
// If path does not contain a leading slash, method is empty, handler is nil or a middleware is nil, Handle panics.
func (r *Router) Handle(path string, method string, handler Handler, middlewares ...Middleware) {
if handler == nil {
panic("handler should not be nil")
}
if method == "" {
panic("method should not be empty")
}
if slices.ContainsFunc(middlewares, func(m Middleware) bool { return m == nil }) {
panic("middlewares should not be nil")
}
handler = transform(transform(handler, middlewares), r.middlewares)
r.router.Handle(method, path, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
request := r.requestPool.Get().(*Request).
WithRequest(req)
for _, param := range params {
request.parameters[param.Key] = param.Value
}
response := handler(request)
if response != nil {
response.Write(w)
}
r.requestPool.Put(request)
})
}
// HandleNotFound registers handler to be called when no matching route is found. By default, Lit uses a
// wrapped http.NotFound.
//
// If handler is nil, HandleNotFound panics.
func (r *Router) HandleNotFound(handler Handler) {
if handler == nil {
panic("handler should not be nil")
}
r.router.NotFound = handler.Base()
}
// HandleOPTIONS registers handler to be called when the request method is OPTIONS. By default, Lit sets the
// Allow header with supported methods.
//
// Useful to support preflight CORS requests, for instance.
//
// If handler is nil, HandleOPTIONS clears the current set handler. In this case, the behaviour is to call the
// registered handler normally, if there is one.
func (r *Router) HandleOPTIONS(handler Handler) {
if handler == nil {
r.router.HandleOPTIONS = false
r.router.GlobalOPTIONS = nil
}
r.router.GlobalOPTIONS = handler.Base()
}
// HandleMethodNotAllowed registers handler to be called when there is a match to a route, but not with that method.
// By default, Lit uses a wrapped http.Error with status code [405 Method Not Allowed].
//
// If handler is nil, HandleMethodNotAllowed clears the current set handler. In this case, the behaviour is to call
// the Not Found handler (either the one defined in HandleNotFound or the default one).
//
// [405 Method Not Allowed]: https://developer.mozilla.org/en-US/docs/web/http/status/405
func (r *Router) HandleMethodNotAllowed(handler Handler) {
if handler == nil {
r.router.HandleMethodNotAllowed = false
return
}
r.router.HandleMethodNotAllowed = true
r.router.MethodNotAllowed = handler.Base()
}
// GET registers handler for path and GET method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "GET", handler, middlewares)
func (r *Router) GET(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodGet, handler, middlewares...)
}
// POST registers handler for path and POST method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "POST", handler, middlewares)
func (r *Router) POST(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodPost, handler, middlewares...)
}
// PUT registers handler for path and PUT method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "PUT", handler, middlewares)
func (r *Router) PUT(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodPut, handler, middlewares...)
}
// PATCH registers handler for path and PATCH method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "PATCH", handler, middlewares)
func (r *Router) PATCH(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodPatch, handler, middlewares...)
}
// DELETE registers handler for path and DELETE method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "DELETE", handler, middlewares)
func (r *Router) DELETE(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodDelete, handler, middlewares...)
}
// OPTIONS registers handler for path and OPTIONS method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "OPTIONS", handler, middlewares)
func (r *Router) OPTIONS(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodOptions, handler, middlewares...)
}
// HEAD registers handler for path and HEAD method and optional local middlewares.
//
// It's equivalent to:
//
// Handle(path, "HEAD", handler, middlewares)
func (r *Router) HEAD(path string, handler Handler, middlewares ...Middleware) {
r.Handle(path, http.MethodHead, handler, middlewares...)
}
// ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL
// and whose method is the same as the request method.
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
r.router.ServeHTTP(writer, request)
}
func transform(handler Handler, middlewares []Middleware) Handler {
for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}
return handler
}