-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponseUtils.go
109 lines (90 loc) · 2.6 KB
/
responseUtils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package january
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"path"
"path/filepath"
)
func (j *January) ReadJson(w http.ResponseWriter, r *http.Request, data interface{}) error {
maxBytes := 1048576 // one megabyte
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytes))
dec := json.NewDecoder(r.Body)
err := dec.Decode(data)
if err != nil {
return err
}
err = dec.Decode(&struct{}{})
if err != io.EOF {
return errors.New("body must only have a single json value")
}
return nil
}
// WriteJson Marshals data
// Sets the appropriate headers
// write the marshaled data to http.ResponseWriter
func (j *January) WriteJson(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error {
out, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return err
}
if len(headers) > 0 {
for key, value := range headers[0] {
w.Header()[key] = value
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
// WriteXML Marshals data
// Sets the appropriate headers
// write the marshaled data to http.ResponseWriter
func (j *January) WriteXML(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error {
out, err := xml.MarshalIndent(data, "", "")
if err != nil {
return err
}
if len(headers) > 0 {
for key, value := range headers[0] {
w.Header()[key] = value
}
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(status)
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
func (j *January) DownloadFile(w http.ResponseWriter, r *http.Request, pathToFile, filename string) error {
fp := path.Join(pathToFile, filename)
fileToServer := filepath.Clean(fp)
//w.Header().Set("Content-Type", fmt.Sprintf("attachment; file=\"%s\"", filename))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; file=\"%s\"", filename))
http.ServeFile(w, r, fileToServer)
return nil
}
func (j *January) Error404(w http.ResponseWriter, r *http.Request) {
j.ErrorStatus(w, r, http.StatusNotFound)
}
func (j *January) Error500(w http.ResponseWriter, r *http.Request) {
j.ErrorStatus(w, r, http.StatusInternalServerError)
}
func (j *January) ErrorUnauthorised(w http.ResponseWriter, r *http.Request) {
j.ErrorStatus(w, r, http.StatusUnauthorized)
}
func (j *January) ErrorForbidden(w http.ResponseWriter, r *http.Request) {
j.ErrorStatus(w, r, http.StatusForbidden)
}
func (j *January) ErrorStatus(w http.ResponseWriter, r *http.Request, statusCode int) {
http.Error(w, http.StatusText(statusCode), statusCode)
}