21 lines
436 B
Go
21 lines
436 B
Go
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})
|
|
}
|