Documentation

Overview

    Package middleware provides several standard middleware implementations.

    Index

    Constants

    View Source
    const RequestIDKey = "reqID"

      Key to use when setting the request ID.

      View Source
      const URLQueryKey string = "urlquery"

        URLQueryKey is the context key for the URL Query

        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 it is nil. While it's impossible in general to ensure that Env is never nil in a middleware stack, in most common cases placing this middleware at the top of the stack will eliminate the need for repetative nil checks.

            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.

                Logger has been designed explicitly to be Good Enough for use in small applications and for people just getting started with Goji. It is expected that applications will eventually outgrow this middleware and replace it with a custom request logger, such as one that produces machine-parseable output, outputs logs to a different service (e.g., syslog), or formats lines like those printed elsewhere in the application.

                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(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.

                        func SubRouter

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

                          SubRouter is a helper middleware that makes writing sub-routers easier.

                          If you register a sub-router under a key like "/admin/*", Goji's router will automatically set c.URLParams["*"] to the unmatched path suffix. This middleware will help you set the request URL's Path to this unmatched suffix, allowing you to write sub-routers with no knowledge of what routes the parent router matches.

                          Since Go's regular expressions do not allow you to create a capturing group named "*", SubRouter also accepts the string "_". For instance, to duplicate the semantics of the string pattern "/foo/*", you might use the regular expression "^/foo(?P<_>/.*)$".

                          This middleware is Match-aware: it will un-set any explicit routing information contained in the Goji context in order to prevent routing loops when using explicit routing with sub-routers. See the documentation for Mux.Router for more.

                          func URLQuery

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

                            URLQuery is a middleware to parse the URL Query parameters just once, and store the resulting url.Values in the context.

                            Types

                            This section is empty.