2022-06-23 00:07:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-06-24 02:22:17 +00:00
|
|
|
"runtime/debug"
|
2022-06-23 00:07:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2022-06-24 02:22:17 +00:00
|
|
|
sendError(w, YggError{Code: 500, Error: "Unspecified error", ErrorMessage: "An error has occured handling your request."})
|
2022-06-23 00:07:14 +00:00
|
|
|
log.Println("error processing:", err)
|
2022-06-24 02:22:17 +00:00
|
|
|
if config.DebugMode {
|
|
|
|
debug.PrintStack()
|
|
|
|
}
|
2022-06-23 00:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendEmpty(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|