34 lines
709 B
Go
34 lines
709 B
Go
package response
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func JSON(w http.ResponseWriter, status int, data any) {
|
|
payload, err := json.Marshal(data)
|
|
if err != nil {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, err := w.Write([]byte(`{"error": "internal server error: failed to marshal response"}`))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_, err = w.Write(payload)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func Error(w http.ResponseWriter, status int, message string) {
|
|
JSON(w, status, map[string]string{"error": message})
|
|
}
|