holiday-api/render.go

59 lines
1.5 KiB
Go
Raw Normal View History

2023-06-16 07:42:50 +00:00
package main
import (
2023-06-20 14:10:46 +00:00
"bytes"
2023-06-16 07:42:50 +00:00
"encoding/json"
2023-06-20 14:10:46 +00:00
"encoding/xml"
"html/template"
2023-06-16 07:42:50 +00:00
"log"
"net/http"
)
2023-06-20 14:10:46 +00:00
func renderTemplate[T any](w http.ResponseWriter, r *http.Request, status int, templates *template.Template, template string, response T) error {
buffer := bytes.Buffer{}
if err := templates.Lookup(template).Execute(&buffer, response); err == nil {
w.Header().Add("content-type", "text/html; charset=utf-8")
2023-06-16 07:42:50 +00:00
w.WriteHeader(status)
2023-06-20 14:10:46 +00:00
_, err = w.Write(buffer.Bytes())
2023-06-16 07:42:50 +00:00
return err
} else {
2023-06-20 14:10:46 +00:00
return onError(w, err)
}
}
func onError(w http.ResponseWriter, err error) error {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("failed writing response: %v", err)
_, _ = w.Write([]byte{})
return err
}
func render[T any](w http.ResponseWriter, r *http.Request, status int, response T) error {
switch r.Header.Get("accept") {
case "application/xml":
fallthrough
case "text/xml":
if body, err := xml.MarshalIndent(response, "", " "); err == nil {
// we return the same type as requested
w.Header().Add("content-type", r.Header.Get("accept")+"; charset=utf-8")
w.WriteHeader(status)
_, err = w.Write([]byte(xml.Header))
_, err = w.Write(body)
return err
} else {
return onError(w, err)
}
case "application/json":
fallthrough
default:
if body, err := json.MarshalIndent(response, "", " "); err == nil {
w.Header().Add("content-type", "application/json; charset=utf-8")
w.WriteHeader(status)
_, err = w.Write(body)
return err
} else {
return onError(w, err)
}
2023-06-16 07:42:50 +00:00
}
}