blob: dbc5882eb2946a498e694c00b6e38965db618b7f [file] [log] [blame]
package rest
import (
"encoding/json"
"net/http"
)
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
// ServeHTTP calls f(w, r) and handles error
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
jsoner(w, http.StatusBadRequest, err.Error()) // TODO detect the correct statusCode from error
}
}
func jsoner(w http.ResponseWriter, statusCode int, payload interface{}) error {
w.Header().Set("Content-Type", "application/json")
// If there is nothing to marshal then set status code and return.
if payload == nil {
_, err := w.Write([]byte("{}"))
return err
}
if statusCode != http.StatusOK {
w.WriteHeader(statusCode)
}
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(true)
encoder.SetIndent("", "")
if err := encoder.Encode(payload); err != nil {
return err
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
return nil
}