middleware

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2016 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDKey = "chi.reqID"

Key to use when setting the request ID.

View Source
const StatusClientClosedRequest = 499

499 Client Closed Request (Nginx) https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Variables

This section is empty.

Functions

func CloseNotify

func CloseNotify(next chi.Handler) chi.Handler

CloseNotify is a middleware that cancels ctx when the underlying connection has gone away. It can be used to cancel long operations on the server when the client disconnects before the response is ready.

func Compress

func Compress(level int, types ...string) func(next http.Handler) http.Handler

CompressLevel is a middleware that compresses response body of a given content types to a data format based on Accept-Encoding request header. It uses a given compression level.

func DefaultCompress

func DefaultCompress(next http.Handler) http.Handler

DefaultCompress is a middleware that compresses response body of predefined content types to a data format based on Accept-Encoding request header. It uses a default compression level.

func GetReqID

func GetReqID(ctx context.Context) string

GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.

func Logger

func Logger(next chi.Handler) chi.Handler

Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white.

Logger prints a request ID if one is provided.

func NoCache

func NoCache(h http.Handler) http.Handler

NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent a router (or subrouter) from being cached by an upstream proxy and/or client.

As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:

Expires: Thu, 01 Jan 1970 00:00:00 UTC
Cache-Control: no-cache, private, max-age=0
X-Accel-Expires: 0
Pragma: no-cache (for HTTP/1.0 proxies/clients)

func RealIP

func RealIP(h http.Handler) http.Handler

RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Forwarded-For header or the X-Real-IP header (in that order).

This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.

You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of Goji. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort).

func Recoverer

func Recoverer(next chi.Handler) chi.Handler

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible.

Recoverer prints a request ID if one is provided.

func RequestID

func RequestID(next chi.Handler) chi.Handler

RequestID is a middleware that injects a request ID into the context of each request. A request ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.

func Throttle

func Throttle(limit int) func(chi.Handler) chi.Handler

Throttle is a middleware that limits number of currently processed requests at a time.

func ThrottleBacklog

func ThrottleBacklog(limit int, backlogLimit int, backlogTimeout time.Duration) func(chi.Handler) chi.Handler

ThrottleBacklog is a middleware that limits number of currently processed requests at a time and provides a backlog for holding a finite number of pending requests.

func Timeout

func Timeout(timeout time.Duration) func(next chi.Handler) chi.Handler

Timeout is a middleware that cancels ctx after a given timeout.

Types

This section is empty.

Jump to

Keyboard shortcuts

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