-
Notifications
You must be signed in to change notification settings - Fork 6
/
encoder.go
89 lines (80 loc) · 2.62 KB
/
encoder.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
package slimhttp
import (
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"net/http"
)
const (
contentTypeKey = "Content-Type"
contentTypeJSON = "application/json; charset=utf-8"
contentTypeXML = "application/xml; charset=utf-8"
contentTypeText = "text/plain; charset=utf-8"
contentTypeHTML = "text/html; charset=utf-8"
)
// An Encoder is a function that will write a response to the passed
// ResponseWriter using the provided statuscode and response struct
type Encoder func(w http.ResponseWriter, status int, res interface{})
// encodeJSON encodes the response as JSON and writes it to the ResponseWriter
func encodeJSON(w http.ResponseWriter, status int, res interface{}) {
w.Header().Set(contentTypeKey, contentTypeJSON)
w.WriteHeader(status)
json.NewEncoder(w).Encode(res)
}
// encodeXML encodes the response as XML and writes it to the ResponseWriter
func encodeXML(w http.ResponseWriter, status int, res interface{}) {
w.Header().Set(contentTypeKey, contentTypeXML)
b, err := xml.Marshal(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Failed to XML marshal response struct")
return
}
w.WriteHeader(status)
fmt.Fprint(w, xml.Header+string(b))
}
// encodeText expects a res of type string, encodes the response as
// Text and writes it to the ResponseWriter
func encodeText(w http.ResponseWriter, status int, res interface{}) {
w.Header().Set(contentTypeKey, contentTypeText)
body, ok := res.(string)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Response type should be (string)")
return
}
w.WriteHeader(status)
fmt.Fprint(w, body)
}
// encodeBytes expects a res interface of type io.Reader and copies
// the bytes read from the reader to the ResponseWriter
func encodeBytes(w http.ResponseWriter, status int, res interface{}) {
body, ok := res.([]byte)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Response type should be ([]byte)")
return
}
w.WriteHeader(status)
w.Write(body)
}
// Webpage represents an HTML webpage response. It contains a template
// and data to be written into the template
type Webpage struct {
Template *template.Template
Data interface{}
}
// encodeHTML expects a res struct of type *template.Template, encodes
// the response as HTML and writes it to the ResponseWriter
func encodeHTML(w http.ResponseWriter, status int, res interface{}) {
w.Header().Set(contentTypeKey, contentTypeHTML)
wp, ok := res.(*Webpage)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Response type should be (*Webpage)")
return
}
w.WriteHeader(status)
wp.Template.Execute(w, wp.Data)
}