32 lines
621 B
Go
32 lines
621 B
Go
//go:build !change
|
|
|
|
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func RespondJSON(w http.ResponseWriter, status int, data interface{}) error {
|
|
response, err := json.Marshal(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
|
|
_, _ = w.Write(response)
|
|
|
|
return nil
|
|
}
|
|
|
|
func ServerError(w http.ResponseWriter) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, _ = w.Write([]byte("Server encountered an error."))
|
|
}
|
|
|
|
func BadRequest(w http.ResponseWriter, message string) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(message))
|
|
}
|