Go Middleware is a collection of standard HTTP middleware packages for Go that you can use individually in your projects. It has three aims in life:

  1. adheres to Go's standard http.Handler interface
  2. uses the stdlib's context package instead of being reliant on an external one
  3. uses minimal dependencies

We have also provided a mux (router) which handles middleware as first-class objects, instead of being relegated to a middleware chain. In all lists of middleware in these docs, the mux (router) is always listed first.

Go's http.Handler Interface

For maximum reusability, we only create middleware which adheres to this interface. Most middleware takes the next middleware and calls it when appropriate. Using this interface means that many other frameworks and routers can use any of the middleware here.

Example frameworks are: [Goji](https://goji.io).

Some example routers are: [Alice](https://github.com/justinas/alice).

Use of the Standard Library's context Package.

Rather than having to rely on something else such as Gorilla's context package, we prefer to use what we have already been given. It also means we don't have to install an extra library too. Since Go v1.7, the context package has been included in the stdlib, so it is beneficial to use it.

Minimal Dependencies

Most middlewares have shallow or no dependencies apart from the standard library. We don't want to bring in the entire jungle for any middleware you need.

We also believe that you shouldn't put all your middleware into one big grab-bag middleware package and instead you just vendor in what you need. Most of these middlewares consist of a code file, a doc file, a ReadMe and a test file. This is smaller than bringing an entire middleware package in and using only 15% of it.

The gomiddleware/mux

We think that middleware is a first-class object and should be used directly, instead of having to create and managed long-winded middleware chains. The gomiddleware/mux allows this.

m := mux.New()

// log all requests
m.Use("/", logger.New())

// add an Id to each request, and then serve the homepage
m.Get("/", reqid.RandomId, homeHandler)

Middleware

By relying on minimal dependencies, the standard library and the standard `context` package, you can be sure that if you vendor in any middleware here it will be both as minimal and as compatible as can be. This is just a start and many more will become available over time.

Each project may provide one or more middlewares you can use in your program. Some projects may provide a simple version, a configurable version and perhaps more advanced version of the same middleware. In most cases you would only use one middleware from each project but of course you are free to use any and as many as you like. Since Go is rock solid for both running and compiling (through the Go v1 compatibility promise) you can be sure that each package is stable, well-tested, and will continue to work for many years.

Other Middleware We Recommend

We don't want to write, or re-write everything ourselves, so we also recommend other packages too. In general, we use other middleware which aligns with our ideals: (i) http.Handler, (ii) uses stdlib's context pkg, (iii) minimal dependencies. Each of these external packages is vetted (briefly) and included here as a recommendation. Please get in touch if you feel a particular pacakge shouldn't be here.

Missing Middleware

If you think some middleware is missing, please create an issue or PR against one of the repos in the GoMiddleware org.

(Ends)