diff options
Diffstat (limited to 'render.go')
-rw-r--r-- | render.go | 27 |
1 files changed, 24 insertions, 3 deletions
@@ -8,7 +8,7 @@ import ( ) type Renderer interface { - Render(context.Context, io.Writer) error + Render(context.Context, http.ResponseWriter) error } func Render(w http.ResponseWriter, r *http.Request, renderable Renderer) { @@ -17,8 +17,29 @@ func Render(w http.ResponseWriter, r *http.Request, renderable Renderer) { } } -type RendererFunc func(context.Context, io.Writer) error +type RendererFunc func(context.Context, http.ResponseWriter) error -func (r RendererFunc) Render(ctx context.Context, w io.Writer) error { +func (r RendererFunc) Render(ctx context.Context, w http.ResponseWriter) error { return r(ctx, w) } + +type IoRenderer interface { + Render(context.Context, io.Writer) error +} + +func Http(code int, inner IoRenderer) Renderer { + return httpWrapper{ + code: code, + inner: inner, + } +} + +type httpWrapper struct { + code int + inner IoRenderer +} + +func (r httpWrapper) Render(ctx context.Context, w http.ResponseWriter) error { + w.WriteHeader(r.code) + return r.inner.Render(ctx, w) +} |