This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
/
render_json.go
166 lines (125 loc) · 3.45 KB
/
render_json.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package middleware
import (
"context"
"encoding/json"
"net/http"
)
type responseKey struct{}
// Response ...
type Response struct {
statusCode int
headers http.Header
payload interface{}
errors []string
}
// ResponseFromContext returns response from context.
func ResponseFromContext(ctx context.Context) *Response {
response := ctx.Value(responseKey{}).(*Response)
return response
}
// SetStatus sets status code to response. If status code is not set if will default to http.StatusOK.
func (r *Response) SetStatus(statusCode int) *Response {
r.statusCode = statusCode
return r
}
// SetStatusBadRequest sets status code to http.StatusBadRequest.
func (r *Response) SetStatusBadRequest(err string) *Response {
r.statusCode = http.StatusBadRequest
if err != "" {
r.AddError(err)
}
return r
}
// SetStatusForbidden sets status code to http.StatusForbidden.
func (r *Response) SetStatusForbidden(err string) *Response {
r.statusCode = http.StatusForbidden
if err != "" {
r.AddError(err)
}
return r
}
// SetStatusInternalServerError sets status code to http.StatusInternalServerError.
func (r *Response) SetStatusInternalServerError(err string) *Response {
r.statusCode = http.StatusInternalServerError
if err != "" {
r.AddError(err)
}
return r
}
// SetStatusNotFound sets status code to http.StatusNotFound.
func (r *Response) SetStatusNotFound(err string) *Response {
r.statusCode = http.StatusNotFound
if err != "" {
r.AddError(err)
}
return r
}
// SetStatusAccepted sets status code to http.StatusAccepted.
func (r *Response) SetStatusAccepted() *Response {
r.statusCode = http.StatusAccepted
return r
}
// SetHeader sets header to response.
func (r *Response) SetHeader(key, value string) *Response {
r.headers.Set(key, value)
return r
}
// AddHeader ads headers to response.
func (r *Response) AddHeader(key, value string) *Response {
r.headers.Add(key, value)
return r
}
// SetPayload sets payload to response.
func (r *Response) SetPayload(payload interface{}) *Response {
r.payload = payload
return r
}
// AddError adds error to response.
func (r *Response) AddError(err string) *Response {
r.errors = append(r.errors, err)
return r
}
// RenderJSON middleware is used to inject response object in context and later render it as JSON.
func RenderJSON(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), responseKey{}, newResponse())
// ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(w, r.WithContext(ctx))
if res, ok := ctx.Value(responseKey{}).(*Response); ok { //nolint:nestif
res.headers.Set("Content-Type", "application/json")
var body []byte
var err error
if res.payload != nil || len(res.errors) > 0 {
b := &response{
Payload: res.payload,
Errors: res.errors,
}
body, err = json.Marshal(b)
if err != nil {
res.SetStatusInternalServerError("Error rendering response body")
}
}
for hk, hsv := range res.headers {
for _, hv := range hsv {
w.Header().Add(hk, hv)
}
}
if res.statusCode > 0 {
w.WriteHeader(res.statusCode)
}
if res.statusCode == http.StatusNoContent {
return
}
_, _ = w.Write(body)
}
})
}
func newResponse() *Response {
return &Response{
headers: make(http.Header, 1),
}
}
type response struct {
Payload interface{} `json:"data,omitempty"`
Errors []string `json:"errors,omitempty"`
}