content

package
v0.0.0-...-10dc113 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package content provides content negotiation handlers for the makross.

Index

Constants

MIME types

View Source
const Language = "Language"

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

Variables

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) makross.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 makross.Context like the following:

func(c *makross.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) makross.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 makross.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 makross.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