-
Notifications
You must be signed in to change notification settings - Fork 0
/
controllers.go
120 lines (102 loc) · 2.64 KB
/
controllers.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
119
120
// Implements a jsonapi for http responses
package jsonapi
import (
"encoding/json"
"net/http"
"compress/gzip"
"github.com/quorumsco/logs"
)
// Returns a Success response
func Success(w http.ResponseWriter, req *http.Request, data interface{}, status int) {
if data == nil {
w.WriteHeader(status)
return
}
b, err := json.Marshal(SuccessView{Status: "success", Data: data})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(b)
}
func SuccessCompress(w http.ResponseWriter, req *http.Request, data interface{}, status int) {
if data == nil {
w.WriteHeader(status)
return
}
b, err := json.Marshal(SuccessView{Status: "success", Data: data})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(status)
gz := gzip.NewWriter(w)
json.NewEncoder(gz).Encode(b)
gz.Close()
//w.Write(b)
}
// SuccessToken returns a success response with a temporary token
func SuccessToken(w http.ResponseWriter, req *http.Request, token string) {
b, err := json.Marshal(SuccessView{Status: "success", Token: token})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}
// Returns a Success response if the data is not nil
func SuccessOKOr404(w http.ResponseWriter, req *http.Request, data interface{}) {
if data == nil {
w.WriteHeader(http.StatusNotFound)
return
}
b, err := json.Marshal(SuccessView{Status: "success", Data: data})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}
// Returns a Fail response
func Fail(w http.ResponseWriter, req *http.Request, data interface{}, status int) {
b, err := json.Marshal(FailView{Status: "fail", Data: data})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(b)
}
// Returns an Error response
func Error(w http.ResponseWriter, req *http.Request, message string, status int) {
b, err := json.Marshal(ErrorView{Status: "error", Message: message})
if err != nil {
logs.Error(err)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(b)
}
// Unmarshaled json data into the passed interface
func Request(data interface{}, req *http.Request) error {
var r = new(RequestView)
var err = json.NewDecoder(req.Body).Decode(&r)
if err != nil {
return err
}
err = json.Unmarshal(r.Data, data)
if err != nil {
return err
}
return nil
}