render

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2022 License: Apache-2.0 Imports: 6 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 a structure that knows how to perform safe HTTP rendering.

func NewRenderer

func NewRenderer() *Renderer

NewRenderer returns an instantiated renderer.

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.

Jump to

Keyboard shortcuts

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