2020-06-23 23:11:01 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2020-10-02 04:16:58 +03:00
|
|
|
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-06-23 23:11:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type j map[string]interface{}
|
|
|
|
|
|
|
|
func internalErrorHandler(w http.ResponseWriter, err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2020-11-15 05:39:53 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(j{"error": err.Error()}); err != nil {
|
|
|
|
internalErrorHandler(w, err)
|
|
|
|
}
|
2020-06-23 23:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func badRequestHandler(w http.ResponseWriter, err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2020-11-15 05:39:53 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(j{"error": err.Error()}); err != nil {
|
|
|
|
internalErrorHandler(w, err)
|
|
|
|
}
|
2020-06-23 23:11:01 +03:00
|
|
|
}
|
2020-10-02 04:16:58 +03:00
|
|
|
|
2020-10-02 09:17:27 +03:00
|
|
|
func WriteSimpleResponse(w http.ResponseWriter, success bool, message string) {
|
2020-10-02 04:16:58 +03:00
|
|
|
response := models.BaseAPIResponse{
|
|
|
|
Success: success,
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-11-15 05:39:53 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
internalErrorHandler(w, err)
|
|
|
|
}
|
2020-10-02 04:16:58 +03:00
|
|
|
}
|