middleware

package
v0.0.0-...-b01cc3d Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2014 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package middleware provides several standard middleware implementations.

Index

Constants

View Source
const OriginalRemoteAddrKey = "originalRemoteAddr"

Key the original value of RemoteAddr is stored under.

View Source
const RequestIDKey = "reqID"

Key to use when setting the request ID.

Variables

This section is empty.

Functions

func AutomaticOptions

func AutomaticOptions(c *web.C, h http.Handler) http.Handler

AutomaticOptions automatically return an appropriate "Allow" header when the request method is OPTIONS and the request would have otherwise been 404'd.

func EnvInit

func EnvInit(c *web.C, h http.Handler) http.Handler

EnvInit is a middleware that allocates an environment map if one does not already exist. This is necessary because Goji does not guarantee that Env is present when running middleware (it avoids forcing the map allocation). Note that other middleware should check Env for nil in order to maximize compatibility (when EnvInit is not used, or when another middleware layer blanks out Env), but for situations in which the user controls the middleware stack and knows EnvInit is present, this middleware can eliminate a lot of boilerplate.

func GetReqID

func GetReqID(c web.C) 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(c *web.C, h http.Handler) http.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 RealIP

func RealIP(c *web.C, 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). It places the original value of RemoteAddr in a context environment variable.

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(c *web.C, h http.Handler) http.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(c *web.C, h http.Handler) http.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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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