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 a structure that knows how to perform safe HTTP rendering.
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.