Documentation ¶
Index ¶
- Constants
- Variables
- type Renderer
- func (r *Renderer) AddOffer(mediaType string, handler func(w http.ResponseWriter)) *Renderer
- func (r *Renderer) Binary(mediaType, filename string, reader io.Reader) *Renderer
- func (r *Renderer) CSV(records [][]string) *Renderer
- func (r *Renderer) CSVChannel(records <-chan []string) *Renderer
- func (r *Renderer) CSVGenerator(recgen func(http.Flusher) ([]string, error)) *Renderer
- func (r *Renderer) CommonFormats(v interface{}) *Renderer
- func (r *Renderer) HALJSON(v interface{}) *Renderer
- func (r *Renderer) HTML(t *template.Template, v interface{}) *Renderer
- func (r *Renderer) IsRendered() bool
- func (r *Renderer) JSON(v interface{}) *Renderer
- func (r *Renderer) Render(w http.ResponseWriter, req *http.Request)
- func (r *Renderer) SetCode(code int) *Renderer
- func (r *Renderer) SetRendered()
- func (r *Renderer) TOML(v interface{}) *Renderer
- func (r *Renderer) Text(t string) *Renderer
- func (r *Renderer) XML(v interface{}, pretty bool) *Renderer
- func (r *Renderer) YAML(v interface{}) *Renderer
Constants ¶
const JSONSecurityPrefix = ")]}',\n"
Variables ¶
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 (*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 ¶
Binary adds a binary file offer for the Renderer struct.
If reader is an io.ReadCloser, it will be closed automatically.
func (*Renderer) CSV ¶
CSV adds a CSV offer for the Renderer object.
Use this function for smaller CSV responses.
func (*Renderer) CSVChannel ¶
CSVChannel adds a CSV offer for the Renderer object.
The records are streamed through a channel.
func (*Renderer) CSVGenerator ¶
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 ¶
CommonFormats adds HAL+JSON, JSON, XML, YAML and TOML offers to the renderer struct.
func (*Renderer) IsRendered ¶
IsRendered checks if the renderer has written its content to an output.
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) SetRendered ¶
func (r *Renderer) SetRendered()
SetRendered marks this Renderer as rendered.
This means that even if Render() will be called, nothing will happen.