Skip to content

Commit

Permalink
Initial HTTPHandler (with panics)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Dec 6, 2023
1 parent bda46e0 commit 4f37b29
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions http_request_renderable.go
Expand Up @@ -19,3 +19,26 @@ type RequestRenderableFunc func(*http.Request) (AsRenderable, error)
func (f RequestRenderableFunc) RequestRenderable(r *http.Request) (AsRenderable, error) {
return f(r)
}

// HTTPHandler implements http.Handler for a RequestRenderable.
type HTTPHandler struct {
Renderable RequestRenderable
}

// ServeHTTP implements http.Handler.
func (h HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
renderable, err := h.Renderable.RequestRenderable(r)
if err != nil {
panic(err)
}

html, err := Render(r.Context(), renderable)
if err != nil {
panic(err)
}

_, err = w.Write([]byte(html))
if err != nil {
panic(err)
}
}

0 comments on commit 4f37b29

Please sign in to comment.