Documentation ¶
Overview ¶
Package render defines rendering functionality.
Index ¶
- type ProdRenderer
- func (r *ProdRenderer) AllowedResponseCode(code int) bool
- func (r *ProdRenderer) RenderCSV(w http.ResponseWriter, code int, filename string, data icsv.Marshaler)
- func (r *ProdRenderer) RenderEmail(tmpl string, data interface{}) ([]byte, error)
- func (r *ProdRenderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
- func (r *ProdRenderer) RenderHTML500(w http.ResponseWriter, err error)
- func (r *ProdRenderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})
- func (r *ProdRenderer) RenderJSON(w http.ResponseWriter, code int, data interface{})
- func (r *ProdRenderer) RenderJSON500(w http.ResponseWriter, err error)
- type Renderer
- type TestRenderer
- func (r *TestRenderer) RenderCSV(w http.ResponseWriter, code int, filename string, data icsv.Marshaler)
- func (r *TestRenderer) RenderEmail(tmpl string, data interface{}) ([]byte, error)
- func (r *TestRenderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
- func (r *TestRenderer) RenderHTML500(w http.ResponseWriter, err error)
- func (r *TestRenderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})
- func (r *TestRenderer) RenderJSON(w http.ResponseWriter, code int, data interface{})
- func (r *TestRenderer) RenderJSON500(w http.ResponseWriter, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ProdRenderer ¶ added in v0.19.0
type ProdRenderer struct {
// contains filtered or unexported fields
}
ProdRenderer is responsible for rendering various content and templates like HTML and JSON responses. This implementation caches templates and uses a pool of buffers.
func (*ProdRenderer) AllowedResponseCode ¶ added in v0.19.0
func (r *ProdRenderer) AllowedResponseCode(code int) bool
AllowedResponseCode returns true if the code is a permitted response code, false otherwise.
func (*ProdRenderer) RenderCSV ¶ added in v0.19.0
func (r *ProdRenderer) 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 (*ProdRenderer) RenderEmail ¶ added in v0.19.0
func (r *ProdRenderer) 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 (*ProdRenderer) RenderHTML ¶ added in v0.19.0
func (r *ProdRenderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
RenderHTML calls RenderHTMLStatus with a http.StatusOK (200).
func (*ProdRenderer) RenderHTML500 ¶ added in v0.19.0
func (r *ProdRenderer) 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 (*ProdRenderer) RenderHTMLStatus ¶ added in v0.19.0
func (r *ProdRenderer) 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 (*ProdRenderer) RenderJSON ¶ added in v0.19.0
func (r *ProdRenderer) 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 (*ProdRenderer) RenderJSON500 ¶ added in v0.19.0
func (r *ProdRenderer) 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.
type Renderer ¶
type Renderer interface { // RenderCSV renders the input as a CSV. RenderCSV(w http.ResponseWriter, code int, filename string, data icsv.Marshaler) // RenderEmail renders the given email HTML template by name. RenderEmail(tmpl string, data interface{}) ([]byte, error) // RenderHTML calls RenderHTMLStatus with a http.StatusOK (200). RenderHTML(w http.ResponseWriter, tmpl string, data interface{}) // RenderHTML500 renders the given error as HTML. RenderHTML500(w http.ResponseWriter, err error) // RenderHTMLStatus renders the given HTML template by name. RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{}) // RenderJSON renders the interface as JSON. RenderJSON(w http.ResponseWriter, code int, data interface{}) // RenderJSON500 renders the given error as JSON. RenderJSON500(w http.ResponseWriter, err error) }
Renderer is responsible for rendering various content and templates like HTML and JSON responses.
type TestRenderer ¶ added in v0.19.0
type TestRenderer struct { *ProdRenderer // the implementation under test T *testing.T }
TestRenderer defines a test version of the renderer
func (*TestRenderer) RenderCSV ¶ added in v0.19.0
func (r *TestRenderer) RenderCSV(w http.ResponseWriter, code int, filename string, data icsv.Marshaler)
func (*TestRenderer) RenderEmail ¶ added in v0.19.0
func (r *TestRenderer) RenderEmail(tmpl string, data interface{}) ([]byte, error)
func (*TestRenderer) RenderHTML ¶ added in v0.19.0
func (r *TestRenderer) RenderHTML(w http.ResponseWriter, tmpl string, data interface{})
func (*TestRenderer) RenderHTML500 ¶ added in v0.19.0
func (r *TestRenderer) RenderHTML500(w http.ResponseWriter, err error)
func (*TestRenderer) RenderHTMLStatus ¶ added in v0.19.0
func (r *TestRenderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{})
func (*TestRenderer) RenderJSON ¶ added in v0.19.0
func (r *TestRenderer) RenderJSON(w http.ResponseWriter, code int, data interface{})
func (*TestRenderer) RenderJSON500 ¶ added in v0.19.0
func (r *TestRenderer) RenderJSON500(w http.ResponseWriter, err error)