mirror of
https://github.com/outbackdingo/matchbox.git
synced 2026-01-27 18:19:36 +00:00
30 lines
644 B
Go
30 lines
644 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
contentType = "Content-Type"
|
|
jsonContentType = "application/json"
|
|
)
|
|
|
|
// renderJSON encodes structs to JSON, writes the response to the
|
|
// ResponseWriter, and logs encoding errors.
|
|
func renderJSON(w http.ResponseWriter, v interface{}) {
|
|
js, err := json.Marshal(v)
|
|
if err != nil {
|
|
log.Errorf("error JSON encoding: %v", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set(contentType, jsonContentType)
|
|
_, err = w.Write(js)
|
|
if err != nil {
|
|
log.Errorf("error writing to response: %v", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|