-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
96 lines (82 loc) · 2.18 KB
/
handler.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
package jsonapi
import (
"errors"
"io"
"net/http"
"reflect"
)
// A JsonHandler wraps a function in
// JsonHandler implements http.Handler. This handler is capable of resolving
// arguments at request time and injecting them into the function. See ArgumentResolver.
//
// For instance, the default maker will inject the context included on the request, and also
// any json body into a struct.
type JsonHandler struct {
fn *reflectedFn // The wrapper over the reflected function
RequestValidator RequestValidator // The validator for the request
ArgumentResolver ArgumentResolver // The argument resolver to be used
SkipPanic bool // Whether to skip panics or not
}
func (h *JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer func(c io.Closer) {
_ = c.Close()
}(req.Body)
// Validate the request
if h.RequestValidator != nil {
items, err := h.RequestValidator.Validate(req)
if errors.Is(err, ErrEmptyBody) {
HandleError(w, req, &apiError{
code: http.StatusBadRequest,
msg: "Request body cannot be empty",
})
return
}
if err != nil {
HandleError(w, req, &apiError{
code: http.StatusInternalServerError,
msg: "There was an error while validating the request",
prev: err,
})
return
}
if len(items) != 0 {
SendResponse(w, req, items)
return
}
}
// arguments is a slice of reflect.Value to pass to h.fn.Call
args := make([]reflect.Value, 0, len(h.fn.in))
for i, t := range h.fn.in {
v, err := h.ArgumentResolver.Resolve(req, t, i)
if errors.Is(err, ErrEmptyBody) {
HandleError(w, req, &apiError{
code: http.StatusBadRequest,
msg: "Request body cannot be empty",
prev: err,
})
return
}
if err != nil {
HandleError(w, req, &apiError{
code: http.StatusInternalServerError,
msg: "Error while trying to resolve handler arguments",
prev: err,
})
return
}
args = append(args, v)
}
defer func() {
if r := recover(); r != nil && h.SkipPanic == false {
err := panicToError(r)
HandleError(w, req, err)
}
}()
out, err := h.fn.call(args)
if err != nil {
HandleError(w, req, err)
return
}
SendResponse(w, req, out)
return
}