-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
90 lines (72 loc) · 1.84 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
package restio
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"github.com/bitrise-io/go-utils/log"
)
// Handler ...
type Handler struct {
F interface{}
}
// Satisfies the http.Handler interface
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
fnValue := reflect.ValueOf(h.F)
argsCount := fnValue.Type().NumIn()
if argsCount > 3 {
respondErrorAndLog(w, "endpoint handlers can have maximum 3 arguments, received: %d", argsCount)
return
}
var args []reflect.Value
if argsCount == 3 {
t := fnValue.Type().In(0)
j := (reflect.New(t).Elem()).Interface()
rv := reflect.ValueOf(&j).Elem()
te := rv.Elem().Type().Elem()
rv.Set(reflect.New(te))
dat, err := ioutil.ReadAll(r.Body)
if err != nil {
respondErrorAndLog(w, "failed to get request body, error: %s", err)
return
}
if err := json.Unmarshal(dat, j); err != nil {
respondErrorAndLog(w, "failed to unmarshal request body, error: %s", err)
return
}
args = append(args, reflect.ValueOf(j))
}
args = append(args, reflect.ValueOf(w))
args = append(args, reflect.ValueOf(r))
fnResults := fnValue.Call(args)
if len(fnResults) > 2 {
respondErrorAndLog(w, "endpoint handlers can have maximum 2 return values, received: %d", len(fnResults))
return
}
var err error
var model interface{}
for _, r := range fnResults {
if r.Type().Implements(errorInterface) {
intf := r.Interface()
if intf != nil {
err = intf.(error)
}
} else {
model = r.Interface()
}
}
if err != nil {
respondErrorAndLog(w, "%s", err)
return
}
if model != nil {
RespondJSON(w, http.StatusOK, model)
}
}
func respondErrorAndLog(w http.ResponseWriter, m string, a ...interface{}) {
m = fmt.Sprintf(m, a...)
RespondError(w, http.StatusInternalServerError, m)
log.Errorf(m)
}