content

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2018 License: MIT Imports: 8 Imported by: 2

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(ctx *fasthttp.RequestCtx, offers []string, defaultOffer string) string

NegotiateContentType returns the best possible response type from a set of options, based on the Accept header.

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 represents the media type.
	Type string
	// Subtype represents the media subtype.
	Subtype string
	// Weight represents the weight (quality factor) of this range.
	Weight float64
	// Parameters represents the parameters that are applicable to this range.
	Parameters map[string]string
	// contains filtered or unexported fields
}

AcceptRange represents a media-range contained within an Accept header.

func AcceptMediaTypes

func AcceptMediaTypes(ctx *fasthttp.RequestCtx) []AcceptRange

AcceptMediaTypes returns the set of accepted media ranges.

func ParseAcceptRange

func ParseAcceptRange(accept string) AcceptRange

ParseAcceptRange returns the media range, params and quality factor (weight) from an Accept range.

func ParseAcceptRanges

func ParseAcceptRanges(accepts string) []AcceptRange

ParseAcceptRanges returns the set of accepted media ranges from an Accept header.

func (AcceptRange) RawString

func (a AcceptRange) RawString() string

RawString returns the raw string for this accept range.

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(h *fasthttp.ResponseHeader)

SetHeader sets the "Content-Type" response header as "text/html; charset=UTF-8"

func (*HTMLDataWriter) Write

func (w *HTMLDataWriter) Write(res io.Writer, data interface{}) error

Write calls routing.DefaultDataWriter to write the given data to the response.

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(h *fasthttp.ResponseHeader)

SetHeader sets the "Content-Type" response header as "application/json".

func (*JSONDataWriter) Write

func (w *JSONDataWriter) Write(res io.Writer, 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(h *fasthttp.ResponseHeader)

SetHeader sets the "Content-Type" response header as "application/xml; charset=UTF-8".

func (*XMLDataWriter) Write

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

Write writes the given data in XML format to the response.

Jump to

Keyboard shortcuts

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