-
Notifications
You must be signed in to change notification settings - Fork 1
/
responce-utils.go
118 lines (105 loc) · 2.97 KB
/
responce-utils.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
110
111
112
113
114
115
116
117
118
package MicroGO
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"path"
"path/filepath"
)
// WriteJson : Create a JSON response.
func (m *MicroGo) 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, val := range headers[0] {
w.Header()[key] = val
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
func (m *MicroGo) ReadJson(w http.ResponseWriter, r *http.Request, data interface{}) error {
maxBytesSize := 1 * 1024 * 1024 //1MB
r.Body = http.MaxBytesReader(w, r.Body, int64(maxBytesSize))
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("the JSON Body need to have only single value. ")
}
return nil
}
// WriteXML : Create XML response.
func (m *MicroGo) 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, val := range headers[0] {
w.Header()[key] = val
}
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(status)
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
// SentFile : Send a file on response.
func (m *MicroGo) SentFile(w http.ResponseWriter, r *http.Request, fileLocation, fileName string) error {
_path := path.Join(fileLocation, fileName)
fileToServe := filepath.Clean(_path)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; file=\"%s\"", fileName))
http.ServeFile(w, r, fileToServe)
return nil
}
// Error404 : Return Not Found HTTP response .
//
// Status Code : 404
func (m *MicroGo) Error404(w http.ResponseWriter, r *http.Request) {
m.ErrorStatus(w, http.StatusNotFound)
}
// Error500 : Return StatusInternal Server Error HTTP response .
//
// Status Code : 500
func (m *MicroGo) Error500(w http.ResponseWriter, r *http.Request) {
m.ErrorStatus(w, http.StatusInternalServerError)
}
// ErrorUnauthorized : Return Unauthorized response on request error.
//
// Status Code : 401
func (m *MicroGo) ErrorUnauthorized(w http.ResponseWriter, r *http.Request) {
m.ErrorStatus(w, http.StatusUnauthorized)
}
// ErrorForbidden : Return StatusForbidden HTTP response.
//
// Status Code : 403
func (m *MicroGo) ErrorForbidden(w http.ResponseWriter, r *http.Request) {
m.ErrorStatus(w, http.StatusForbidden)
}
// ErrorUnprocessable : Return Unprocessable entity HTTP response.
//
// Status Code 422.
func (m *MicroGo) ErrorUnprocessable(w http.ResponseWriter, r *http.Request) {
m.ErrorStatus(w, http.StatusUnprocessableEntity)
}
// ErrorStatus : Construct Error HTTP response
func (m *MicroGo) ErrorStatus(w http.ResponseWriter, status int) {
http.Error(w, http.StatusText(status), status)
}