diff --git a/backend/cmd/api/json.go b/backend/cmd/api/json.go new file mode 100644 index 0000000..c6d93fb --- /dev/null +++ b/backend/cmd/api/json.go @@ -0,0 +1,20 @@ +package main + +import ( + "encoding/json" + "net/http" +) + +func writeJSON(w http.ResponseWriter, status int, data any) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + return json.NewEncoder(w).Encode(data) +} + +func writeJSONError(w http.ResponseWriter, status int, message string) error { + type envelope struct { + Error string `json:"error"` + } + + return writeJSON(w, status, &envelope{Error: message}) +}