Render writes the response to the provided http.ResponseWriter.
(w http.ResponseWriter)
| 156 | |
| 157 | // Render writes the response to the provided http.ResponseWriter. |
| 158 | func (r *syncResponse) Render(w http.ResponseWriter) error { |
| 159 | // Set an appropriate ETag header |
| 160 | if r.etag != nil { |
| 161 | etag, err := localUtil.EtagHash(r.etag) |
| 162 | if err == nil { |
| 163 | w.Header().Set("ETag", fmt.Sprintf("\"%s\"", etag)) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if r.headers != nil { |
| 168 | for h, v := range r.headers { |
| 169 | w.Header().Set(h, v) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if r.location != "" { |
| 174 | w.Header().Set("Location", r.location) |
| 175 | if r.code == 0 { |
| 176 | r.code = 201 |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Handle plain text headers. |
| 181 | if r.plaintext { |
| 182 | w.Header().Set("Content-Type", "text/plain") |
| 183 | } |
| 184 | |
| 185 | // Handle compression. |
| 186 | if r.compress { |
| 187 | w.Header().Set("Content-Encoding", "gzip") |
| 188 | } |
| 189 | |
| 190 | // Write header and status code. |
| 191 | if r.code == 0 { |
| 192 | r.code = http.StatusOK |
| 193 | } |
| 194 | |
| 195 | if w.Header().Get("Connection") != "keep-alive" { |
| 196 | w.WriteHeader(r.code) |
| 197 | } |
| 198 | |
| 199 | // Prepare the JSON response |
| 200 | status := api.Success |
| 201 | if !r.success { |
| 202 | status = api.Failure |
| 203 | |
| 204 | // If the metadata is an error, consider the response a SmartError |
| 205 | // to propagate the data and preserve the status code. |
| 206 | err, ok := r.metadata.(error) |
| 207 | if ok { |
| 208 | return SmartError(err).Render(w) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Handle plain text responses. |
| 213 | if r.plaintext { |
| 214 | if r.metadata != nil { |
| 215 | if r.compress { |
nothing calls this directly
no test coverage detected