22 lines
661 B
Go
22 lines
661 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// writeJSON encodes v as JSON and writes it to w with the given status code.
|
|
// This is a tiny helper that every handler uses — it keeps handlers clean.
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
|
http.Error(w, "encoding error", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// writeError writes a standard {"error": "..."} JSON response.
|
|
func writeError(w http.ResponseWriter, status int, msg string) {
|
|
writeJSON(w, status, map[string]string{"error": msg})
|
|
}
|