forked from go-chi/chi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
81 lines (67 loc) · 1.93 KB
/
render.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
package render
import (
"bytes"
"encoding/json"
"encoding/xml"
"net/http"
"reflect"
)
func String(w http.ResponseWriter, status int, v string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(status)
w.Write([]byte(v))
}
func HTML(w http.ResponseWriter, status int, v string) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
w.Write([]byte(v))
}
func JSON(w http.ResponseWriter, status int, v interface{}) {
b, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(b) > 0 {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
w.Write(b)
}
func Noop(w http.ResponseWriter) {
String(w, http.StatusOK, "")
}
func XML(w http.ResponseWriter, status int, v interface{}) {
b, err := xml.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.WriteHeader(status)
// Try to find <?xml header in first 100 bytes (just in case there're some XML comments).
findHeaderUntil := len(b)
if findHeaderUntil > 100 {
findHeaderUntil = 100
}
if bytes.Index(b[:findHeaderUntil], []byte("<?xml")) == -1 {
// No header found. Print it out first.
w.Write([]byte(xml.Header))
}
w.Write(b)
}
func Respond(w http.ResponseWriter, status int, v interface{}) {
if err, ok := v.(error); ok {
JSON(w, status, map[string]interface{}{"error": err.Error()})
return
}
// Force to return empty JSON array [] instead of null in case of zero slice.
val := reflect.ValueOf(v)
if val.Kind() == reflect.Slice && val.IsNil() {
v = reflect.MakeSlice(val.Type(), 0, 0).Interface()
}
JSON(w, status, v)
}