package main import ( "encoding/json" "io/ioutil" "log" "net/http" ) 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) { switch err.Error() { case "unexpected end of JSON input": sendError(w, YggError{Code: 400, Error: "Bad Request", ErrorMessage: "The request data is malformed."}) default: sendError(w, YggError{Code: 500, Error: "Unspecified error", ErrorMessage: "An error has occured handling your request."}) } log.Println("error processing:", err) } func sendEmpty(w http.ResponseWriter) { w.WriteHeader(http.StatusNoContent) }