hoisie/web — pull request #152 by ysimonson (2013-07-22 13:01:05+00:00)
Title: Express.js-like middleware

This adds support for express.js-like middleware. With this patch, multiple functions can be called for any given endpoint, which allows you add functionality in a compositional manner.

You can specify global middleware that is called for all requests:

web.Middleware(gzip)
web.Middleware(basicAuth)

// This endpoint will now call the following functions in order: gzip,
// basicAuth, sayHello
web.Get("/", sayHello)

And you can specify middleware on a per-endpoint basis:

// Functionally the same as the above, although other endpoints specified
// will not have gzip and basicAuth called now.
web.Get("/", gzip, basicAuth, sayHello)

Not every function in the chain has to be called. A function can "bail" the request and circumvent the execution of further middleware by calling `ctx.Finish()`. e.g. if `basicAuth` in the above examples set some headers and called `ctx.Finish()` because the authentication failed, `sayHello` will not be called.

Unit tests have been added as well, although I'm not confident that this patch is bug-free. A second pair of eyes would help greatly since I'm not too familiar with this codebase, especially the altered logic in `routeHandler`.