Files
matchbox/api/serialize.go
2016-02-01 22:09:39 -08:00

49 lines
1.0 KiB
Go

package api
import (
"encoding/json"
"io"
"net/http"
"text/template"
)
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)
}
}
func renderTemplate(w io.Writer, data interface{}, contents ...string) (err error) {
tmpl := template.New("")
for _, content := range contents {
tmpl, err = tmpl.Parse(content)
if err != nil {
log.Errorf("error parsing template: %v", err)
return err
}
}
err = tmpl.Execute(w, data)
if err != nil {
log.Errorf("error rendering template: %v", err)
return err
}
return nil
}