render

package
v0.33.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2021 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package render defines rendering functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer is responsible for rendering various content and templates like HTML and JSON responses. This implementation caches templates and uses a pool of buffers.

func New

func New(ctx context.Context, fsys fs.FS, debug bool) (*Renderer, error)

New creates a new renderer with the given details.

func (*Renderer) AllowedResponseCode added in v0.3.0

func (r *Renderer) AllowedResponseCode(code int) bool

AllowedResponseCode returns true if the code is a permitted response code, false otherwise.

func (*Renderer) RenderCSV added in v0.17.1

func (r *Renderer) RenderCSV(w http.ResponseWriter, code int, filename string, data icsv.Marshaler)

RenderCSV renders the input as a CSV. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.

func (*Renderer) RenderEmail added in v0.12.0

func (r *Renderer) RenderEmail(tmpl string, data interface{}) ([]byte, error)

RenderEmail renders the given email HTML template by name. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.

The buffers are fetched via a sync.Pool to reduce allocations and improve performance.

func (*Renderer) RenderHTML

func (r *Renderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})

RenderHTML calls RenderHTMLStatus with a http.StatusOK (200).

func (*Renderer) RenderHTML500 added in v0.19.0

func (r *Renderer) RenderHTML500(w http.ResponseWriter, err error)

RenderHTML500 renders the given error as HTML. In production mode, this always renders a generic "server error" message. In debug, it returns the actual error from the caller.

func (*Renderer) RenderHTMLStatus

func (r *Renderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})

RenderHTMLStatus renders the given HTML template by name. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.

If template rendering fails, a generic 500 page is returned. In dev mode, the error is included on the page. If flushing the buffer to the response fails, an error is logged, but no recovery is attempted.

The buffers are fetched via a sync.Pool to reduce allocations and improve performance.

func (*Renderer) RenderJSON

func (r *Renderer) RenderJSON(w http.ResponseWriter, code int, data interface{})

RenderJSON renders the interface as JSON. It attempts to gracefully handle any rendering errors to avoid partial responses sent to the response by writing to a buffer first, then flushing the buffer to the response.

If the provided data is nil and the response code is a 200, the result will be `{"ok":true}`. If the code is not a 200, the response will be of the format `{"error":"<val>"}` where val is the JSON-escaped http.StatusText for the provided code.

If rendering fails, a generic 500 JSON response is returned. In dev mode, the error is included in the payload. If flushing the buffer to the response fails, an error is logged, but no recovery is attempted.

The buffers are fetched via a sync.Pool to reduce allocations and improve performance.

func (*Renderer) RenderJSON500 added in v0.19.0

func (r *Renderer) RenderJSON500(w http.ResponseWriter, err error)

RenderJSON500 renders the given error as JSON. In production mode, this always renders a generic "server error" message. In debug, it returns the actual error from the caller.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL