holiday-api/render.go

22 lines
483 B
Go
Raw Normal View History

2023-06-16 07:42:50 +00:00
package main
import (
"encoding/json"
"log"
"net/http"
)
func render[T any](w http.ResponseWriter, r *http.Request, status int, response T) error {
if body, err := json.MarshalIndent(response, "", " "); err == nil {
w.Header().Add("content-type", "application/json")
w.WriteHeader(status)
_, err = w.Write(body)
return err
} else {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("couldn't parse response")
_, err = w.Write([]byte{})
return err
}
}