Documentation
¶
Overview ¶
Package mw provides middleware for the github.com/go-on/stack package
Index ¶
- Variables
- func After(h http.Handler) *after
- func Around(before, after http.Handler) *around
- func Before(h http.Handler) *before
- func Caller() (info string)
- func Catch(fn func(recovered interface{}, w http.ResponseWriter, r *http.Request)) *catch
- func DELETEHandler(path string, handler http.Handler) *matchHandler
- func Defer(h http.Handler) *defer_
- func Fallback(ignoreCodes []int, handler ...http.Handler) *fallback
- func FilterBody(m method.Method) *filterBody
- func First(handler ...http.Handler) *first
- func GETHandler(path string, handler http.Handler) *matchHandler
- func GZip(w http.ResponseWriter, r *http.Request, next http.Handler)
- func GetError(ctx stack.Contexter) error
- func Guard(h http.Handler) *guard
- func HEADHandler(path string, handler http.Handler) *matchHandler
- func Head() head
- func IfMatch(h http.Handler) *ifMatch
- func JSONRequest(ptr interface{}, r *http.Request) (err error)
- func JSONResponse(obj interface{}, w http.ResponseWriter) (err error)
- func MethodOverride() methodOverride
- func OPTIONSHandler(path string, handler http.Handler) *matchHandler
- func PATCHHandler(path string, handler http.Handler) *matchHandler
- func POSTHandler(path string, handler http.Handler) *matchHandler
- func PUTHandler(path string, handler http.Handler) *matchHandler
- func PanicCodes(codes ...int) panicCodes
- func Prepare() prepare
- func ReadSeeker(r io.ReadSeeker) *readseeker
- func RequestHeader(key, val string) *requestHeader
- func ResponseHeader(key, val string) *responseHeader
- func SetError(err error, ctx stack.Contexter)
- func Switch(decider func(r *http.Request) bool, ...) *switcher
- type Attachment
- type CSSString
- type Compress
- type ContentType
- type Deflate
- type DelRequestHeader
- type DelResponseHeader
- type DispatchFunc
- type Dispatcher
- type Error
- type ErrorHandler
- type HTMLString
- type JSONString
- type JavaScriptString
- type MatchFunc
- type MatchHost
- type MatchMethod
- type MatchPath
- type MatchScheme
- type Matcher
- type MethodHandler
- type MethodOverrideByField
- type PermanentRedirect
- type String
- type TemporaryRedirect
- type TextString
Constants ¶
This section is empty.
Variables ¶
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") )
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.
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
var HTTP1_1 = http1_1{}
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.
var Logger = logger{}
var ResponseWriterHandler = responseWriterHandle{}
var Stop = stop{}
Stop is a wrapper that does no processing but simply prevents further execution of next wrappers
Functions ¶
func Around ¶
Around returns a wrapper that calls the given before and after handler before and after the next handler when serving
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 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 GETHandler ¶
func GZip ¶
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 HEADHandler ¶
func Head ¶
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 ¶
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 ¶
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 PATCHHandler ¶
func POSTHandler ¶
func PUTHandler ¶
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
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
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
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 ¶
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) ServeHTTP ¶
func (df DispatchFunc) ServeHTTP(wr http.ResponseWriter, req *http.Request, next 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)
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
type MatchMethod ¶
type MatchMethod string
MatchMethod matches based on the request method (like GET, POST etc.)
type Matcher ¶
Matcher is an interface for types that can match against a http.Request
func And ¶
And logically combines different matchers to a single matcher that only matches if all matchers match.
func MatchHeader ¶
MatchHeader matches based on the request header
func MatchHostRegex ¶
MatchHostRegex matches the request host against a reqular expression
func MatchPathRegex ¶
MatchPathRegex matches the request path against a reqular expression
func MatchQuery ¶
MatchQuery matches based on the request query
type MethodHandler ¶
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 ¶
func (m MethodOverrideByField) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.Handler)
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
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
Source Files
¶
- after.go
- around.go
- attachment.go
- before.go
- catch.go
- compress.go
- content_type.go
- defer.go
- del_request_header.go
- del_response_header.go
- dispatch.go
- doc.go
- error.go
- escape_html.go
- etag.go
- fallback.go
- filter_body.go
- first.go
- guard.go
- head.go
- helper.go
- http-1-1.go
- json.go
- log.go
- match.go
- method_override.go
- prepare.go
- readseeker.go
- request_header.go
- response_header.go
- responsewriterhandler.go
- stop.go
- string.go
- switch.go