-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrequest.go
377 lines (318 loc) · 8.96 KB
/
request.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package bunrouter
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
type routeCtxKey struct{}
// ParamsFromContext retrieves route parameters from the given context.
// It returns an empty Params if no parameters are found.
func ParamsFromContext(ctx context.Context) Params {
if ctx == nil {
return Params{}
}
route, _ := ctx.Value(routeCtxKey{}).(Params)
return route
}
// contextWithParams stores route parameters in the context.
func contextWithParams(ctx context.Context, params Params) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, routeCtxKey{}, params)
}
//------------------------------------------------------------------------------
// HTTPHandler converts http.Handler to bunrouter.HandlerFunc.
func HTTPHandler(handler http.Handler) HandlerFunc {
if handler == nil {
panic("bunrouter: nil handler")
}
return HTTPHandlerFunc(handler.ServeHTTP)
}
// HTTPHandlerFunc converts http.HandlerFunc to bunrouter.HandlerFunc.
func HTTPHandlerFunc(handler http.HandlerFunc) HandlerFunc {
if handler == nil {
panic("bunrouter: nil handler")
}
return func(w http.ResponseWriter, req Request) (err error) {
if w == nil {
return fmt.Errorf("bunrouter: nil response writer")
}
ctx := contextWithParams(req.Context(), req.params)
defer func() {
if v := recover(); v != nil {
var ok bool
err, ok = v.(error)
if !ok {
err = fmt.Errorf("bunrouter: panic recovered: %v", v)
}
}
}()
handler.ServeHTTP(w, req.Request.WithContext(ctx))
return err
}
}
// HandlerFunc is a function that handles HTTP requests in bunrouter.
// It returns an error that will be handled by the router.
type HandlerFunc func(w http.ResponseWriter, req Request) error
var _ http.Handler = (*HandlerFunc)(nil)
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if h == nil {
http.Error(w, "Handler not found", http.StatusInternalServerError)
return
}
if req == nil {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
if err := h(w, NewRequest(req)); err != nil {
code := http.StatusInternalServerError
if httpErr, ok := err.(HTTPError); ok {
code = httpErr.StatusCode()
}
http.Error(w, err.Error(), code)
}
}
// HTTPError represents an HTTP error with a status code
type HTTPError interface {
error
StatusCode() int
}
// MiddlewareFunc is a function that wraps a HandlerFunc to provide middleware functionality.
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
//------------------------------------------------------------------------------
// Request extends http.Request with route parameters.
type Request struct {
*http.Request
params Params
}
// NewRequest creates a new Request instance from an http.Request.
func NewRequest(req *http.Request) Request {
if req == nil {
req = &http.Request{}
}
return Request{
Request: req,
params: ParamsFromContext(req.Context()),
}
}
func newRequestParams(req *http.Request, params Params) Request {
if req == nil {
req = &http.Request{}
}
return Request{
Request: req,
params: params,
}
}
// WithContext returns a new Request with the provided context.
func (req Request) WithContext(ctx context.Context) Request {
if ctx == nil {
ctx = context.Background()
}
return Request{
Request: req.Request.WithContext(ctx),
params: req.params,
}
}
// Params returns the route parameters associated with the request.
func (req Request) Params() Params {
return req.params
}
// Param returns the value of the named parameter or empty string if not found.
func (req Request) Param(key string) string {
return req.Params().ByName(key)
}
// Route returns the matched route pattern.
func (req Request) Route() string {
return req.Params().Route()
}
//------------------------------------------------------------------------------
// Params holds route parameters and route information.
type Params struct {
path string
node *node
handler *routeHandler
wildcardLen uint16
}
// IsZero returns true if Params has no associated route node.
func (ps Params) IsZero() bool {
return ps.node == nil
}
// Route returns the route pattern that matched the request.
func (ps Params) Route() string {
if ps.node != nil {
return ps.node.route
}
return ""
}
// Get returns the value of the named parameter and whether it was found.
func (ps Params) Get(name string) (string, bool) {
if ps.node == nil || ps.handler == nil {
return "", false
}
if i, ok := ps.handler.params[name]; ok {
return ps.findParam(i)
}
return "", false
}
func (ps *Params) findParam(paramIndex int) (string, bool) {
if ps.node == nil || ps.handler == nil {
return "", false
}
path := ps.path
pathLen := len(path)
if pathLen == 0 {
return "", false
}
currNode := ps.node
currParamIndex := len(ps.handler.params) - 1
if paramIndex < 0 || paramIndex > currParamIndex {
return "", false
}
// Wildcard can be only in the final node.
if ps.node.isWC {
if currParamIndex == paramIndex {
if int(ps.wildcardLen) > pathLen {
return "", false
}
pathLen -= int(ps.wildcardLen)
return path[pathLen:], true
}
currParamIndex--
if int(ps.wildcardLen) > pathLen {
return "", false
}
pathLen -= int(ps.wildcardLen)
path = path[:pathLen]
}
for currNode != nil {
if currNode.part[0] != ':' { // static node
partLen := len(currNode.part)
if partLen > pathLen {
return "", false
}
pathLen -= partLen
path = path[:pathLen]
currNode = currNode.parent
continue
}
i := strings.LastIndexByte(path, '/')
if i == -1 {
return "", false
}
pathLen = i + 1
if currParamIndex == paramIndex {
return path[pathLen:], true
}
currParamIndex--
path = path[:pathLen]
currNode = currNode.parent
}
return "", false
}
// ByName returns the value of the named parameter or empty string if not found.
func (ps Params) ByName(name string) string {
s, _ := ps.Get(name)
return s
}
// Int parses the named parameter as an integer.
func (ps Params) Int(name string) (int, error) {
value := ps.ByName(name)
if value == "" {
return 0, fmt.Errorf("bunrouter: param '%s' not found", name)
}
return strconv.Atoi(value)
}
// Uint32 parses the named parameter as an unsigned 32-bit integer.
func (ps Params) Uint32(name string) (uint32, error) {
value := ps.ByName(name)
if value == "" {
return 0, fmt.Errorf("bunrouter: param '%s' not found", name)
}
n, err := strconv.ParseUint(value, 10, 32)
return uint32(n), err
}
// Uint64 parses the named parameter as an unsigned 64-bit integer.
func (ps Params) Uint64(name string) (uint64, error) {
value := ps.ByName(name)
if value == "" {
return 0, fmt.Errorf("bunrouter: param '%s' not found", name)
}
return strconv.ParseUint(value, 10, 64)
}
// Int32 parses the named parameter as a signed 32-bit integer.
func (ps Params) Int32(name string) (int32, error) {
value := ps.ByName(name)
if value == "" {
return 0, fmt.Errorf("bunrouter: param '%s' not found", name)
}
n, err := strconv.ParseInt(value, 10, 32)
return int32(n), err
}
// Int64 parses the named parameter as a signed 64-bit integer.
func (ps Params) Int64(name string) (int64, error) {
value := ps.ByName(name)
if value == "" {
return 0, fmt.Errorf("bunrouter: param '%s' not found", name)
}
return strconv.ParseInt(value, 10, 64)
}
// Map returns route parameters as a map[string]string.
func (ps Params) Map() map[string]string {
if ps.handler == nil || len(ps.handler.params) == 0 {
return make(map[string]string)
}
m := make(map[string]string, len(ps.handler.params))
for param, index := range ps.handler.params {
if value, ok := ps.findParam(index); ok {
m[param] = value
}
}
return m
}
// Param represents a key-value pair of route parameters.
type Param struct {
Key string
Value string
}
// Slice returns route parameters as a slice of Param.
func (ps Params) Slice() []Param {
if ps.handler == nil || len(ps.handler.params) == 0 {
return []Param{}
}
slice := make([]Param, len(ps.handler.params))
for param, index := range ps.handler.params {
if value, ok := ps.findParam(index); ok {
slice[index] = Param{Key: param, Value: value}
}
}
return slice
}
//------------------------------------------------------------------------------
// H is a shorthand for map[string]interface{}.
type H map[string]interface{}
// JSON marshals the value as JSON and writes it to the response writer.
// It sets the Content-Type header to application/json.
//
// Don't hesitate to copy-paste this function to your project and customize it as necessary.
func JSON(w http.ResponseWriter, value interface{}) error {
if w == nil {
return fmt.Errorf("bunrouter: nil response writer")
}
w.Header().Set("Content-Type", "application/json")
if value == nil {
if _, err := w.Write([]byte("null")); err != nil {
return fmt.Errorf("bunrouter: failed to write null response: %w", err)
}
return nil
}
enc := json.NewEncoder(w)
if err := enc.Encode(value); err != nil {
return fmt.Errorf("bunrouter: JSON encoding error: %w", err)
}
return nil
}