Some basic helper functions for writing a json back to the client
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
parent
adb8263505
commit
9cd57daf4b
20
backend/cmd/api/json.go
Normal file
20
backend/cmd/api/json.go
Normal file
@ -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})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user