openreplay/backend/internal/http/router/response.go
2022-06-06 14:13:24 +02:00

24 lines
463 B
Go

package router
import (
"encoding/json"
"log"
"net/http"
)
func ResponseWithJSON(w http.ResponseWriter, res interface{}) {
body, err := json.Marshal(res)
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}
func ResponseWithError(w http.ResponseWriter, code int, err error) {
type response struct {
Error string `json:"error"`
}
w.WriteHeader(code)
ResponseWithJSON(w, &response{err.Error()})
}