33 lines
558 B
Go
33 lines
558 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
|
|
dat, err := json.Marshal(payload)
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
return
|
|
}
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
w.Write(dat)
|
|
}
|
|
|
|
func respondWithError(w http.ResponseWriter, code int, msg string) {
|
|
if code > 499 {
|
|
log.Println("Error: ", msg)
|
|
}
|
|
|
|
type errorResponse struct {
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
respondWithJSON(w, code, errorResponse{
|
|
Error: msg,
|
|
})
|
|
}
|