mw

package
v6.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2015 License: MIT Imports: 17 Imported by: 7

Documentation

Overview

Package mw provides middleware for the github.com/go-on/stack package

Index

Constants

This section is empty.

Variables

View Source
var (
	JSONContentType       = ContentType("application/json; charset=utf-8")
	TextContentType       = ContentType("text/plain; charset=utf-8")
	CSSContentType        = ContentType("text/css; charset=utf-8")
	HTMLContentType       = ContentType("text/html; charset=utf-8")
	JavaScriptContentType = ContentType("application/javascript; charset=utf-8")
	RSSFeedContentType    = ContentType("application/rss+xml; charset=utf-8")
	AtomFeedContentType   = ContentType("application/atom+xml; charset=utf-8")
	PDFContentType        = ContentType("application/pdf")
	CSVContentType        = ContentType("text/csv; charset=utf-8")
)
View Source
var ETag = etag{}

ETag buffers the body writes of the next handler and calculates a md5 Hash based on the Content-Type + Body combination and sets it as etag in the response header. It does so only for GET and HEAD requests. For GET requests the buffered body is flushed to the underlying response writer.

View Source
var EscapeHTML = escapeHTML{}

EscapeHTML wraps the next handler by replacing the response writer with an EscapeHTMLResponseWriter that escapes html special chars while writing to the underlying response writer

View Source
var HTTP1_1 = http1_1{}
View Source
var IfNoneMatch = ifNoneMatch{}

IfNoneMatch only acts upon HEAD and GET requests that have a If-None-Match header set. It then runs the next handler and checks if an Etag header is set and if it matches the If-None-Match. It it does, the status code 304 (not modified) is sent (without body). Otherwise the response is flushed as is. The combination of Etag and IfNoneMatch can be used to trigger effective client side caching. But IfNoneMatch may also be used with a custom handler that sets the ETag header.

View Source
var Logger = logger{}
View Source
var ResponseWriterHandler = responseWriterHandle{}
View Source
var Stop = stop{}

Stop is a wrapper that does no processing but simply prevents further execution of next wrappers

Functions

func After

func After(h http.Handler) *after

func Around

func Around(before, after http.Handler) *around

Around returns a wrapper that calls the given before and after handler before and after the next handler when serving

func Before

func Before(h http.Handler) *before

func Caller

func Caller() (info string)

func Catch

func Catch(fn func(recovered interface{}, w http.ResponseWriter, r *http.Request)) *catch

Catch returns a CatchFunc for a Catcher

func DELETEHandler

func DELETEHandler(path string, handler http.Handler) *matchHandler

func Defer

func Defer(h http.Handler) *defer_

func Fallback

func Fallback(ignoreCodes []int, handler ...http.Handler) *fallback

Fallback will try all given handler until the first one writes to the ResponseWriter body. It is similar to First, but ignores writes that did set one of the ignore status codes

func FilterBody

func FilterBody(m method.Method) *filterBody

Filter the body for the given method

func First

func First(handler ...http.Handler) *first

First will try all given handler until the first one returns something

func GETHandler

func GETHandler(path string, handler http.Handler) *matchHandler

func GZip

func GZip(w http.ResponseWriter, r *http.Request, next http.Handler)

GZip compresses the body written by the next handlers on the fly if the client did set the Accept-Encoding header to gzip. It also sets the response header Content-Encoding to gzip if it did the compression.

func GetError

func GetError(ctx stack.Contexter) error

GetError gets the error out of the given stack.Contexter. If there is no error, nil is returned.

func Guard

func Guard(h http.Handler) *guard

Guard returns a GuardFunc for a http.Handler

func HEADHandler

func HEADHandler(path string, handler http.Handler) *matchHandler
func Head() head

Head will check if the request method is HEAD. if so, it will change the method to GET, call the handler with a ResponseBuffer and return only the header information to the client.

For non HEAD methods, it simply pass the request handling through to the http.handler

func IfMatch

func IfMatch(h http.Handler) *ifMatch

IfMatch only acts upon GET, PUT, DELETE or PATCH requests, that have the If-Match header set. It then issues a HEAD request to the given handler in order to get the etag from the header and compares the etag to the one given via the If-Match header. If it matches, the next handler is called, otherwise status 412 (precondition failed) is returned IfMatch may be used in combination with a middleware stack that uses ETag or any custom handler that sets the ETag header.

func JSONRequest

func JSONRequest(ptr interface{}, r *http.Request) (err error)

JSONRequest unmarshals the http.Request body to the object of the pointer ptr

func JSONResponse

func JSONResponse(obj interface{}, w http.ResponseWriter) (err error)

JSONResponse mashals the given obj to the given http.ResponseWriter

func MethodOverride

func MethodOverride() methodOverride

func OPTIONSHandler

func OPTIONSHandler(path string, handler http.Handler) *matchHandler

func PATCHHandler

func PATCHHandler(path string, handler http.Handler) *matchHandler

func POSTHandler

func POSTHandler(path string, handler http.Handler) *matchHandler

func PUTHandler

func PUTHandler(path string, handler http.Handler) *matchHandler

func PanicCodes

func PanicCodes(codes ...int) panicCodes

PanicCodes returns a middleware that panics if any of the given http codes is set. This allows to get stack traces for debugging. Don't use this in production.

func Prepare

func Prepare() prepare

PrepareLikeMux prepares a request the same way that net/http ServeMux does

func ReadSeeker

func ReadSeeker(r io.ReadSeeker) *readseeker

ReadSeeker provides a wrap.Wrapper for a io.ReadSeeker

func RequestHeader

func RequestHeader(key, val string) *requestHeader

SetRequestHeader sets a request header

func ResponseHeader

func ResponseHeader(key, val string) *responseHeader

ResponseHeader sets a response header

func SetError

func SetError(err error, ctx stack.Contexter)

SetError stores the given error inside the given stack.Contexter if err is not nil

func Switch

func Switch(decider func(r *http.Request) bool, truemw, falsemw func(w http.ResponseWriter, r *http.Request, next http.Handler)) *switcher

Switch switches between 2 middleware functions via call of a decider function If the decider function returns true, truemw is run, otherwise the falsemw

Types

type Attachment

type Attachment string

func (Attachment) ServeHTTP

func (a Attachment) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Attachment) Set

func (a Attachment) Set(w http.ResponseWriter)

type CSSString

type CSSString string

CSSString is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (CSSString) ServeHTTP

func (t CSSString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the CSSString to the http.ResponseWriter and sets Content-Type header to text/css; charset=utf-8

type Compress

type Compress int

Compress compresses the body written by the next handlers via either gzip or deflate, if the client supports it. When deflate is used the int is used as compression level

func (Compress) ServeHTTP

func (d Compress) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Handler)

type ContentType

type ContentType string

ContentType writes the content type if the next handler was successful and did not set a content-type

func (ContentType) ServeHTTP

func (c ContentType) ServeHTTP(wr http.ResponseWriter, req *http.Request, next http.Handler)

ServeHandle serves the given request with the next handler and after that writes the content type, if the next handler was successful and did not set a content-type

func (ContentType) Set

func (c ContentType) Set(w http.ResponseWriter)

SetContentType sets the content type in the given ResponseWriter

type Deflate

type Deflate int

Deflate compresses the body written by the next handlers with the level of the int

func (Deflate) ServeHTTP

func (d Deflate) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Handler)

type DelRequestHeader

type DelRequestHeader string

DelRequestHeader removes request headers that are identical to the string or have it as prefix

func (DelRequestHeader) ServeHTTPNext

func (rh DelRequestHeader) ServeHTTPNext(w http.ResponseWriter, r *http.Request, next http.Handler)

ServeHTTPNext removes request headers that are identical to the string or have it as prefix. Then the inner http.Handler is called

type DelResponseHeader

type DelResponseHeader string

DelResponseHeader removes response headers that are identical to the string or have if as prefix

func (DelResponseHeader) ServeHTTP

func (rh DelResponseHeader) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Handler)

ServeHTTP removes the response headers that are identical to the string or have if as prefix after the next handler is run

type DispatchFunc

type DispatchFunc func(*http.Request) http.Handler

func Dispatch

func Dispatch(d Dispatcher) DispatchFunc

func Map

func Map(data ...interface{}) DispatchFunc

data should be pairs of Matcher and http.Handler

func (DispatchFunc) Dispatch

func (df DispatchFunc) Dispatch(r *http.Request) http.Handler

func (DispatchFunc) ServeHTTP

func (df DispatchFunc) ServeHTTP(wr http.ResponseWriter, req *http.Request, next http.Handler)

type Dispatcher

type Dispatcher interface {
	Dispatch(*http.Request) http.Handler
}

type Error

type Error struct {
	Err error
}

Error a type based on error that should be saved by a stack.Contexter (response writer)

func (*Error) Reclaim

func (e *Error) Reclaim(repl interface{})

Reclaim implements the stack.Reclaimer interface.

type ErrorHandler

type ErrorHandler func(error, http.ResponseWriter, *http.Request)

ErrorHandler is a function that handles an error, if an error has been set inside the stack.Contexter via SetError(). It acts as a middleware that passes to the next http.Handler if there is no error inside the Contexter, otherwise it handles the error by calling the function

func (ErrorHandler) ServeHTTP

func (fn ErrorHandler) ServeHTTP(ctx stack.Contexter, rw http.ResponseWriter, req *http.Request, next http.Handler)

type HTMLString

type HTMLString string

HTMLString is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (HTMLString) ServeHTTP

func (t HTMLString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the HTMLString to the http.ResponseWriter and sets Content-Type header to text/html; charset=utf-8

type JSONString

type JSONString string

JSONString is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (JSONString) ServeHTTP

func (t JSONString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the JSONString to the http.ResponseWriter and sets Content-Type header to application/json; charset=utf-8

type JavaScriptString

type JavaScriptString string

JavaScriptString is a type alias for string that is a http.Handler and a wrap.Wrapper

func (JavaScriptString) ServeHTTP

func (t JavaScriptString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the JavaScriptString to the http.ResponseWriter and sets Content-Type header to application/javascript; charset=utf-8

func (JavaScriptString) Wrap

Wrap implements the wrap.Wrapper interface

type MatchFunc

type MatchFunc func(*http.Request) bool

MatchFunc is a Matcher based on a function

func (MatchFunc) Match

func (mf MatchFunc) Match(req *http.Request) bool

Match implements Matcher for the MatchFunc

type MatchHost

type MatchHost string

MatchHost matches based on the request host

func (MatchHost) Match

func (mh MatchHost) Match(r *http.Request) bool

type MatchMethod

type MatchMethod string

MatchMethod matches based on the request method (like GET, POST etc.)

func (MatchMethod) Match

func (mh MatchMethod) Match(r *http.Request) bool

type MatchPath

type MatchPath string

MatchMethod matches based on the request path

func (MatchPath) Match

func (mh MatchPath) Match(r *http.Request) bool

type MatchScheme

type MatchScheme string

MatchScheme matches based on the request scheme

func (MatchScheme) Match

func (m MatchScheme) Match(r *http.Request) bool

type Matcher

type Matcher interface {
	// Match returns if the request matches
	Match(*http.Request) bool
}

Matcher is an interface for types that can match against a http.Request

func And

func And(ms ...Matcher) Matcher

And logically combines different matchers to a single matcher that only matches if all matchers match.

func MatchHeader

func MatchHeader(key, value string) Matcher

MatchHeader matches based on the request header

func MatchHostRegex

func MatchHostRegex(r *regexp.Regexp) Matcher

MatchHostRegex matches the request host against a reqular expression

func MatchPathRegex

func MatchPathRegex(r *regexp.Regexp) Matcher

MatchPathRegex matches the request path against a reqular expression

func MatchQuery

func MatchQuery(key, val string) Matcher

MatchQuery matches based on the request query

func Or

func Or(ms ...Matcher) Matcher

And logically combines different matchers to a single matcher that matches if one of the matchers match.

type MethodHandler

type MethodHandler struct {
	GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD http.Handler
}

func (*MethodHandler) ServeHTTPNext

func (mh *MethodHandler) ServeHTTPNext(wr http.ResponseWriter, req *http.Request, next http.Handler)

type MethodOverrideByField

type MethodOverrideByField string

MethodOverrideByField overrides the request method by looking for a field that contains the target method. It only acts on POST requests and on post bodies.

func (MethodOverrideByField) ServeHTTP

type PermanentRedirect

type PermanentRedirect string

func (PermanentRedirect) ServeHTTP

func (t PermanentRedirect) ServeHTTP(wr http.ResponseWriter, req *http.Request)

type String

type String string

String is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (String) ServeHTTP

func (s String) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the String to the http.ResponseWriter

type TemporaryRedirect

type TemporaryRedirect string

func (TemporaryRedirect) ServeHTTP

func (t TemporaryRedirect) ServeHTTP(wr http.ResponseWriter, req *http.Request)

type TextString

type TextString string

TextString is an utf-8 string that is a http.Handler and a wrap.Wrapper

func (TextString) ServeHTTP

func (t TextString) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP writes the TextString to the http.ResponseWriter and sets Content-Type header to text/plain; charset=utf-8

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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