forked from wish/eventmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
63 lines (54 loc) · 1.57 KB
/
adapter.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
package jh
import (
"encoding/json"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
// JSONHandler is the function interface used so that an Adapter can
// automatically parse out status codes and set headers.
//
// The first parameter is sent out as encoded json if error is nil, otherwise
// an error response is sent out as json.
type JSONHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (interface{}, error)
// Adapter is middleware that converts a JSONHandler route into a normal
// http.HandlerFunc.
//
// It inspects the return against a collection of interfaces to determine
// status codes and what to encode out as json. It correctly sets status code,
// and content-type.
func Adapter(jh JSONHandler) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
r, err := jh(w, req, ps)
if err != nil {
status := http.StatusInternalServerError
switch err := err.(type) {
case Error:
status = err.Status()
}
w.WriteHeader(status)
r := map[string]string{"error": err.Error()}
if err := json.NewEncoder(w).Encode(&r); err != nil {
log.Printf("json encode: %v", err)
}
return
}
var d interface{}
switch s := r.(type) {
case Success:
d = s.Data()
default:
d = r
}
status := http.StatusOK
switch s := r.(type) {
case HasStatus:
status = s.Status()
}
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(&d); err != nil {
log.Printf("%v: json encode failure: %+v", req.URL.Path, err)
}
}
}