kinda working

This commit is contained in:
wagslane
2023-05-30 21:28:23 -06:00
parent 7d1852d380
commit 80ec635705
91 changed files with 13103 additions and 2 deletions

31
json.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"encoding/json"
"log"
"net/http"
)
func respondWithError(w http.ResponseWriter, code int, msg string) {
if code > 499 {
log.Printf("Responding with 5XX error: %s", msg)
}
type errorResponse struct {
Error string `json:"error"`
}
respondWithJSON(w, code, errorResponse{
Error: msg,
})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
dat, err := json.Marshal(payload)
if err != nil {
log.Printf("Error marshalling JSON: %s", err)
w.WriteHeader(500)
return
}
w.WriteHeader(code)
w.Write(dat)
}