-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
66 lines (54 loc) · 1.5 KB
/
json.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
package httpapi
import (
"encoding/json"
"errors"
"fmt"
"log"
"mime"
"net/http"
"reflect"
)
type returnHandlerFunc func(*http.Request) (int, interface{})
type jsonResponse struct {
Code int `json:"code"`
Description string `json:"description"`
Debug string `json:"debug,omitempty"`
}
func withJSONResponse(next returnHandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code, body := next(r)
if err, ok := body.(error); ok || body == nil {
resp := jsonResponse{Code: code, Description: http.StatusText(code)}
body = resp
if err != nil {
(r.Context().Value(contextKeyLogData)).(*logData).Error = err.Error()
if er, ok := err.(*errResponse); ok {
body = er
} else if Debug {
resp.Debug = err.Error()
}
}
}
w.Header().Set(headerContentType, "application/json")
w.WriteHeader(code)
e := json.NewEncoder(w)
err := e.Encode(body)
if err != nil {
log.Println("Error writing JSON response:", err)
}
})
}
func jsonRequest(r *http.Request, v interface{}) error {
mediaType, _, err := mime.ParseMediaType(r.Header.Get(headerContentType))
if err != nil {
return fmt.Errorf("Could not parse Content-Type: %v", err)
}
if mediaType != mediaTypeJSON {
return errors.New("Content-Type not application/json")
}
dec := json.NewDecoder(r.Body)
if err := dec.Decode(v); err != nil {
return fmt.Errorf("Unable to parse request body to %s: %v", reflect.TypeOf(v).Elem().Name(), err)
}
return nil
}