forked from TripwireTeam/tripwire
44 lines
895 B
Go
44 lines
895 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"runtime/debug"
|
|
)
|
|
|
|
func sendJSON(w http.ResponseWriter, jsonMessage interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(jsonMessage)
|
|
}
|
|
|
|
func sendError(w http.ResponseWriter, err YggError) {
|
|
w.WriteHeader(err.Code)
|
|
sendJSON(w, err)
|
|
}
|
|
|
|
func unmarshalTo(r *http.Request, v any) error {
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = json.Unmarshal(body, &v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func handleError(w http.ResponseWriter, err error) {
|
|
sendError(w, YggError{Code: 500, Error: "Unspecified error", ErrorMessage: "An error has occured handling your request."})
|
|
log.Println("error processing:", err)
|
|
if config.DebugMode {
|
|
debug.PrintStack()
|
|
}
|
|
}
|
|
|
|
func sendEmpty(w http.ResponseWriter) {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|