blob: 9f47bffb4c21e3cecf16d93a0369b43a104cce96 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
// Package http provides adapters to render gomponents in http handlers.
package http
import (
"net/http"
g "github.com/maragudk/gomponents"
)
// Handler is like http.Handler but returns a Node and an error.
// See Adapt for how errors are translated to HTTP responses.
type Handler = func(http.ResponseWriter, *http.Request) (g.Node, error)
type errorWithStatusCode interface {
StatusCode() int
}
// Adapt a Handler to a http.Handlerfunc.
// The returned Node is rendered to the ResponseWriter, in both normal and error cases.
// If the Handler returns an error, and it implements a "StatusCode() int" method, that HTTP status code is sent
// in the response header. Otherwise, the status code http.StatusInternalServerError (500) is used.
func Adapt(h Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
n, err := h(w, r)
if err != nil {
switch v := err.(type) {
case errorWithStatusCode:
w.WriteHeader(v.StatusCode())
default:
w.WriteHeader(http.StatusInternalServerError)
}
}
if n == nil {
return
}
if err := n.Render(w); err != nil {
http.Error(w, "error rendering node: "+err.Error(), http.StatusInternalServerError)
}
}
}
|