23 lines
470 B
Go
23 lines
470 B
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func RespondJSON(w http.ResponseWriter, status int, payload any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if payload == nil {
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(payload)
|
|
}
|
|
|
|
func RespondError(w http.ResponseWriter, status int, message string) {
|
|
RespondJSON(w, status, ErrorResponse{Message: message})
|
|
}
|