my fork of Gabes awesome self hosted live stream publishing software https://github.com/owncast/owncast
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

69 lines
1.6 KiB

package controllers
import (
"encoding/json"
"net/http"
"github.com/owncast/owncast/models"
log "github.com/sirupsen/logrus"
)
type j map[string]interface{}
// InternalErrorHandler will return an error message as an HTTP response.
func InternalErrorHandler(w http.ResponseWriter, err error) {
if err == nil {
return
}
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode(j{"error": err.Error()}); err != nil {
InternalErrorHandler(w, err)
}
}
// BadRequestHandler will return an HTTP 500 as an HTTP response.
func BadRequestHandler(w http.ResponseWriter, err error) {
if err == nil {
return
}
log.Debugln(err)
w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(j{"error": err.Error()}); err != nil {
InternalErrorHandler(w, err)
}
}
// WriteSimpleResponse will return a message as a response.
func WriteSimpleResponse(w http.ResponseWriter, success bool, message string) {
response := models.BaseAPIResponse{
Success: success,
Message: message,
}
w.Header().Set("Content-Type", "application/json")
if success {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
}
if err := json.NewEncoder(w).Encode(response); err != nil {
InternalErrorHandler(w, err)
}
}
// WriteResponse will return an object as a JSON encoded response.
func WriteResponse(w http.ResponseWriter, response interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
InternalErrorHandler(w, err)
}
}