-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
56 lines (47 loc) · 1.91 KB
/
response.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
package nibbler
import (
"encoding/json"
"net/http"
)
// WriteJson is some syntactic sugar to allow for a quick way to write JSON responses with a status code
func WriteJson(w http.ResponseWriter, content string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write([]byte(content))
}
// WriteJson is some syntactic sugar to allow for a quick way to write JSON responses from structs with a status code
func WriteStructToJson(w http.ResponseWriter, content interface{}, code int) {
r, err := json.Marshal(content)
if err != nil {
Write500Json(w, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(r)
}
// Write200Json is some syntactic sugar to allow for a quick way to write JSON responses with an OK code
func Write200Json(w http.ResponseWriter, content string) {
WriteJson(w, content, http.StatusOK)
}
// Write201Json is some syntactic sugar to allow for a quick way to write JSON responses with an OK code
func Write201Json(w http.ResponseWriter, content string) {
WriteJson(w, content, http.StatusCreated)
}
// Write204 is some syntactic sugar to allow for a quick way to write JSON responses with a no-content code
func Write204(w http.ResponseWriter, content string) {
w.WriteHeader(http.StatusNoContent)
w.Write(nil)
}
// Write401Json
func Write401Json(w http.ResponseWriter) {
WriteJson(w, `{"result": "not authorized"}`, http.StatusUnauthorized)
}
// Write404Json is some syntactic sugar to allow for a quick way to write JSON responses with a StatusNotFound code
func Write404Json(w http.ResponseWriter) {
WriteJson(w, `{"result": "not found"}`, http.StatusNotFound)
}
// Write500Json is some syntactic sugar to allow for a quick way to write JSON responses with an InternalServererror code
func Write500Json(w http.ResponseWriter, message string) {
WriteJson(w, `{"result": "` + message + `"}`, http.StatusInternalServerError)
}