@@ -136,24 +136,40 @@ func RouteNotFound(rw http.ResponseWriter) {
136
136
// marshaling, such as the number of elements in an array, which could help us
137
137
// spot routes that need to be paginated.
138
138
func Write (ctx context.Context , rw http.ResponseWriter , status int , response interface {}) {
139
+ // Pretty up JSON when testing.
140
+ if flag .Lookup ("test.v" ) != nil {
141
+ WriteIndent (ctx , rw , status , response )
142
+ return
143
+ }
144
+
139
145
_ , span := tracing .StartSpan (ctx )
140
146
defer span .End ()
141
147
142
- buf := & bytes.Buffer {}
143
- enc := json .NewEncoder (buf )
148
+ rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
149
+ rw .WriteHeader (status )
150
+
151
+ enc := json .NewEncoder (rw )
144
152
enc .SetEscapeHTML (true )
145
- // Pretty up JSON when testing.
146
- if flag .Lookup ("test.v" ) != nil {
147
- enc .SetIndent ("" , "\t " )
148
- }
153
+
149
154
err := enc .Encode (response )
150
155
if err != nil {
151
156
http .Error (rw , err .Error (), http .StatusInternalServerError )
152
157
return
153
158
}
159
+ }
160
+
161
+ func WriteIndent (ctx context.Context , rw http.ResponseWriter , status int , response interface {}) {
162
+ _ , span := tracing .StartSpan (ctx )
163
+ defer span .End ()
164
+
154
165
rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
155
166
rw .WriteHeader (status )
156
- _ , err = rw .Write (buf .Bytes ())
167
+
168
+ enc := json .NewEncoder (rw )
169
+ enc .SetEscapeHTML (true )
170
+ enc .SetIndent ("" , "\t " )
171
+
172
+ err := enc .Encode (response )
157
173
if err != nil {
158
174
http .Error (rw , err .Error (), http .StatusInternalServerError )
159
175
return
0 commit comments