render

package
v0.0.0-...-83625b3 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const JSONSecurityPrefix = ")]}',\n"

Variables

View Source
var JSONPrefix = true

JSONPrefix is a global switch for the ")]}',\n" JSON response prefix.

This prefix increases security for browser-based applications, but requires extra support on the client side.

Functions

This section is empty.

Types

type Renderer

type Renderer struct {
	Code int // HTTP status code.
	// contains filtered or unexported fields
}

Renderer is a per-request struct for the Render API.

The Render API handles content negotiation with the client. The server's preference is the order how the offers are added by either the AddOffer() low-level method or the JSON()/HTML()/Text() higher level methods.

A quick example how to use the Render API:

func pageHandler(w http.ResponseWriter, r *http.Request) {
    ...
    ab.Render(r).
        HTML(pageTemplate, data).
        JSON(data)
}

In this example, the server prefers rendering an HTML page / fragment, but it can render a JSON if that's the client's preference. The default is HTML, because that is the first offer.

func NewRenderer

func NewRenderer() *Renderer

NewRenderer creates a new Renderer.

func (*Renderer) AddOffer

func (r *Renderer) AddOffer(mediaType string, handler func(w http.ResponseWriter)) *Renderer

AddOffer adds an offer for the content negotiation.

See the Render() method for more information. The mediaType is the content type, the handler renders the data to the ResponseWriter. You probably want to use the JSON(), HTML(), Text() methods instead of this.

func (*Renderer) Binary

func (r *Renderer) Binary(mediaType, filename string, reader io.Reader) *Renderer

Binary adds a binary file offer for the Renderer struct.

If reader is an io.ReadCloser, it will be closed automatically.

func (*Renderer) CSV

func (r *Renderer) CSV(records [][]string) *Renderer

CSV adds a CSV offer for the Renderer object.

Use this function for smaller CSV responses.

func (*Renderer) CSVChannel

func (r *Renderer) CSVChannel(records <-chan []string) *Renderer

CSVChannel adds a CSV offer for the Renderer object.

The records are streamed through a channel.

func (*Renderer) CSVGenerator

func (r *Renderer) CSVGenerator(recgen func(http.Flusher) ([]string, error)) *Renderer

CSVGenerator adds a CSV offer for the Renderer object.

The records are generated with a generator function. If the function returns an error, the streaming to the output stops.

func (*Renderer) CommonFormats

func (r *Renderer) CommonFormats(v interface{}) *Renderer

CommonFormats adds HAL+JSON, JSON, XML, YAML and TOML offers to the renderer struct.

func (*Renderer) HALJSON

func (r *Renderer) HALJSON(v interface{}) *Renderer

HALJSON adds a HAL+JSON offer to the Renderer struct.

func (*Renderer) HTML

func (r *Renderer) HTML(t *template.Template, v interface{}) *Renderer

HTML adds an HTML offer to the Renderer struct.

func (*Renderer) IsRendered

func (r *Renderer) IsRendered() bool

IsRendered checks if the renderer has written its content to an output.

func (*Renderer) JSON

func (r *Renderer) JSON(v interface{}) *Renderer

JSON adds a JSON offer to the Renderer struct.

func (*Renderer) Render

func (r *Renderer) Render(w http.ResponseWriter, req *http.Request)

Render renders the best offer to the ResponseWriter according to the client's content type preferences.

func (*Renderer) SetCode

func (r *Renderer) SetCode(code int) *Renderer

SetCode sets the HTTP status code.

func (*Renderer) SetRendered

func (r *Renderer) SetRendered()

SetRendered marks this Renderer as rendered.

This means that even if Render() will be called, nothing will happen.

func (*Renderer) TOML

func (r *Renderer) TOML(v interface{}) *Renderer

TOML adds a TOML offer to the Renderer struct.

func (*Renderer) Text

func (r *Renderer) Text(t string) *Renderer

Text adds a plain text offer to the Renderer struct.

func (*Renderer) XML

func (r *Renderer) XML(v interface{}, pretty bool) *Renderer

XML adds XML offer to the Renderer object.

If pretty is set, the XML will be indented. Also text/xml content type header will be sent instead of application/xml.

func (*Renderer) YAML

func (r *Renderer) YAML(v interface{}) *Renderer

YAML adds a YAML offer to the Renderer struct.

Jump to

Keyboard shortcuts

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