pure package - github.com/go-playground/pure - Go Packages (original) (raw)

View Source

const ( ApplicationJSON = "application/json" ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8 ApplicationJavaScript = "application/javascript" ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8 ApplicationXML = "application/xml" ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8 ApplicationForm = "application/x-www-form-urlencoded" ApplicationQueryParams = "" ApplicationProtobuf = "application/protobuf" ApplicationMsgpack = "application/msgpack" TextHTML = "text/html" TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8 TextPlain = "text/plain" TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8 MultipartForm = "multipart/form-data" OctetStream = "application/octet-stream"

CharsetUTF8 = "charset=utf-8"

AcceptedLanguage   = "Accept-Language"
AcceptEncoding     = "Accept-Encoding"
ContentDisposition = "Content-Disposition"
ContentEncoding    = "Content-Encoding"
ContentLength      = "Content-Length"
ContentType        = "Content-Type"
Location           = "Location"
Upgrade            = "Upgrade"
Vary               = "Vary"
WWWAuthenticate    = "WWW-Authenticate"
XForwardedFor      = "X-Forwarded-For"
XRealIP            = "X-Real-Ip"
Allow              = "Allow"
Origin             = "Origin"

Gzip = "gzip"

WildcardParam = "*wildcard"

)

HTTP Constant Terms and Variables

This section is empty.

AcceptedLanguages returns an array of accepted languages denoted by the Accept-Language header sent by the browser NOTE: some stupid browsers send in locales lowercase when all the rest send it properly

Attachment is a helper method for returning an attachement file to be downloaded, if you with to open inline see function Inline

ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.

Decode takes the request and attempts to discover it's content type via the http headers and then decode the request body into the provided struct. Example if header was "application/json" would decode using json.NewDecoder(io.LimitReader(r.Body, maxMemory)).Decode(v).

NOTE: when includeQueryParams=true both query params and SEO query params will be parsed and included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and added to the request.Form prior to decoding or added to parsed JSON or XML; in short SEO query params are treated just like normal query params.

DecodeForm parses the requests form data into the provided struct.

The Content-Type and http method are not checked.

NOTE: when includeQueryParams=true both query params and SEO query params will be parsed and included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and added to the request.Form prior to decoding; in short SEO query params are treated just like normal query params.

DecodeJSON decodes the request body into the provided struct and limits the request size via an io.LimitReader using the maxMemory param.

The Content-Type e.g. "application/json" and http method are not checked.

NOTE: when includeQueryParams=true both query params and SEO query params will be parsed and included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and added to parsed JSON; in short SEO query params are treated just like normal query params.

DecodeMultipartForm parses the requests form data into the provided struct.

The Content-Type and http method are not checked.

NOTE: when includeQueryParams=true both query params and SEO query params will be parsed and included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and added to the request.Form prior to decoding; in short SEO query params are treated just like normal query params.

DecodeQueryParams takes the URL Query params, adds SEO params or not based on the includeSEOQueryParams flag.

NOTE: DecodeQueryParams is also used/called from Decode when no ContentType is specified the only difference is that it will always pass true for includeSEOQueryParams

DecodeSEOQueryParams decodes the SEO Query params only and ignores the normal URL Query params.

DecodeXML decodes the request body into the provided struct and limits the request size via an io.LimitReader using the maxMemory param.

The Content-Type e.g. "application/xml" and http method are not checked.

NOTE: when includeQueryParams=true both query params and SEO query params will be parsed and included eg. route /user/:id?test=true both 'id' and 'test' are treated as query params and added to parsed XML; in short SEO query params are treated just like normal query params.

Inline is a helper method for returning a file inline to be rendered/opened by the browser

JSON marshals provided interface + returns JSON + status code

JSONBytes returns provided JSON response with status code

JSONP sends a JSONP response with status code and uses `callback` to construct the JSONP payload.

ParseForm calls the underlying http.Request ParseForm but also adds the URL params to the request Form as if they were defined as query params i.e. ?id=13&ok=true but does not add the params to the http.Request.URL.RawQuery for SEO purposes

ParseMultipartForm calls the underlying http.Request ParseMultipartForm but also adds the URL params to the request Form as if they were defined as query params i.e. ?id=13&ok=true but does not add the params to the http.Request.URL.RawQuery for SEO purposes

QueryParams returns the r.URL.Query() values and optionally have them include the SEO query params eg. route /users/:id?test=val if includeSEOQueryParams=true then values will include 'id' and 'test' values

XML marshals provided interface + returns XML + status code

XMLBytes returns provided XML response with status code

FormDecoder is the type used for decoding a form for use

type IRouteGroup interface { IRoutes GroupWithNone(prefix string) IRouteGroup GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup Group(prefix string) IRouteGroup }

IRouteGroup interface for router group

type IRoutes interface { Use(...Middleware) Any(string, http.HandlerFunc) Get(string, http.HandlerFunc) Post(string, http.HandlerFunc) Delete(string, http.HandlerFunc) Patch(string, http.HandlerFunc) Put(string, http.HandlerFunc) Options(string, http.HandlerFunc) Head(string, http.HandlerFunc) Connect(string, http.HandlerFunc) Trace(string, http.HandlerFunc) }

IRoutes interface for routes

Middleware is pure's middleware definition

Mux is the main request multiplexer

New Creates and returns a new Pure instance

Any adds a route & handler to the router for all HTTP methods.

Connect adds a CONNECT route & handler to the router.

Delete adds a DELETE route & handler to the router.

Get adds a GET route & handler to the router.

Group creates a new sub router with specified prefix and retains existing middleware.

func (g *Mux) GroupWithMore(prefix string, middleware ...Middleware) IRouteGroup

GroupWithMore creates a new sub router with specified prefix, retains existing middleware and adds new middleware.

func (g *Mux) GroupWithNone(prefix string) IRouteGroup

GroupWithNone creates a new sub router with specified prefix and no middleware attached.

func (*Mux) Handle

Handle allows for any method to be registered with the given route & handler. Allows for non standard methods to be used like CalDavs PROPFIND and so forth.

Head adds a HEAD route & handler to the router.

Match adds a route & handler to the router for multiple HTTP methods provided.

Options adds an OPTIONS route & handler to the router.

Patch adds a PATCH route & handler to the router.

Post adds a POST route & handler to the router.

Put adds a PUT route & handler to the router.

Register404 alows for overriding of the not found handler function. NOTE: this is run after not finding a route even after redirecting with the trailing slash

func (p *Mux) RegisterAutomaticOPTIONS(middleware ...Middleware)

RegisterAutomaticOPTIONS tells pure whether to automatically handle OPTION requests; manually configured OPTION handlers take precedence. default true

func (p *Mux) RegisterMethodNotAllowed(middleware ...Middleware)

RegisterMethodNotAllowed tells pure whether to handle the http 405 Method Not Allowed status code

Serve returns an http.Handler to be used.

func (p *Mux) SetRedirectTrailingSlash(set bool)

SetRedirectTrailingSlash tells pure whether to try and fix a URL by trying to find it lowercase -> with or without slash -> 404

Trace adds a TRACE route & handler to the router.

func (g *Mux) Use(m ...Middleware)

Use adds a middleware handler to the group middleware chain.

ReqVars is the interface of request scoped variables tracked by pure

RequestVars returns the request scoped variables tracked by pure