content

package
v2.1.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2018 License: MIT Imports: 7 Imported by: 43

Documentation

Overview

Package content provides content negotiation handlers for the ozzo routing package.

Index

Constants

View Source
const (
	JSON = routing.MIME_JSON
	XML  = routing.MIME_XML
	XML2 = routing.MIME_XML2
	HTML = routing.MIME_HTML
)

MIME types

View Source
const Language = "Language"

Language is the key used to store and retrieve the chosen language in routing.Context

Variables

View Source
var DataWriters = map[string]routing.DataWriter{
	JSON: &JSONDataWriter{},
	XML:  &XMLDataWriter{},
	XML2: &XMLDataWriter{},
	HTML: &HTMLDataWriter{},
}

DataWriters lists all supported content types and the corresponding data writers. By default, JSON, XML, and HTML are supported. You may modify this variable before calling TypeNegotiator to customize supported data writers.

Functions

func LanguageNegotiator

func LanguageNegotiator(languages ...string) routing.Handler

LanguageNegotiator returns a content language negotiation handler.

The method takes a list of languages (locale IDs) that are supported by the application. The negotiator will determine the best language to use by checking the Accept-Language request header. If no match is found, the first language will be used.

In a handler, you can access the chosen language through routing.Context like the following:

func(c *routing.Context) error {
    language := c.Get(content.Language).(string)
}

If you do not specify languages, the negotiator will set the language to be "en-US".

func NegotiateContentType

func NegotiateContentType(r *http.Request, offers []string, defaultOffer string) string

func TypeNegotiator

func TypeNegotiator(formats ...string) routing.Handler

TypeNegotiator returns a content type negotiation handler.

The method takes a list of response MIME types that are supported by the application. The negotiator will determine the best response MIME type to use by checking the "Accept" HTTP header. If no match is found, the first MIME type will be used.

The negotiator will set the "Content-Type" response header as the chosen MIME type. It will call routing.Context.SetDataWriter() to set the appropriate data writer that can write data in the negotiated format.

If you do not specify any supported MIME types, the negotiator will use "text/html" as the response MIME type.

Types

type AcceptRange

type AcceptRange struct {
	Type       string
	Subtype    string
	Weight     float64
	Parameters map[string]string
	// contains filtered or unexported fields
}

func AcceptMediaTypes

func AcceptMediaTypes(r *http.Request) []AcceptRange

func ParseAcceptRange

func ParseAcceptRange(accept string) AcceptRange

func ParseAcceptRanges

func ParseAcceptRanges(accepts string) []AcceptRange

func (AcceptRange) RawString

func (a AcceptRange) RawString() string

type HTMLDataWriter

type HTMLDataWriter struct{}

HTMLDataWriter sets the "Content-Type" response header as "text/html; charset=UTF-8" and calls routing.DefaultDataWriter to write the given data to the response.

func (*HTMLDataWriter) SetHeader

func (w *HTMLDataWriter) SetHeader(res http.ResponseWriter)

func (*HTMLDataWriter) Write

func (w *HTMLDataWriter) Write(res http.ResponseWriter, data interface{}) error

type JSONDataWriter

type JSONDataWriter struct{}

JSONDataWriter sets the "Content-Type" response header as "application/json" and writes the given data in JSON format to the response.

func (*JSONDataWriter) SetHeader

func (w *JSONDataWriter) SetHeader(res http.ResponseWriter)

func (*JSONDataWriter) Write

func (w *JSONDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)

type XMLDataWriter

type XMLDataWriter struct{}

XMLDataWriter sets the "Content-Type" response header as "application/xml; charset=UTF-8" and writes the given data in XML format to the response.

func (*XMLDataWriter) SetHeader

func (w *XMLDataWriter) SetHeader(res http.ResponseWriter)

func (*XMLDataWriter) Write

func (w *XMLDataWriter) Write(res http.ResponseWriter, data interface{}) (err error)

Jump to

Keyboard shortcuts

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