Jon Crosby has an excellent talk from Mountain West Ruby Conference 2009 about how middleware is taking over web development. His article “A World of Middleware” is a bit difficult to parse if you don’t know Rack, so I’ll try to simplify what he’s saying a bit…
Rails 2.3 is built on Rack, which is a simple specification to connect web frameworks like Rails and Merb with web servers like Mongrel and Thin. Rails 2.3 also lets you define middleware code to be run before Rails is invoked, again using Rack. In Rails terms, middlewares are like before_filter’s that are run before Rails is ever even called. Another way of looking at a middleware is like a miniature application that can connect, via Rack, to another middleware. Each middleware stack handles certain requests (for example, certain url’s can be handled by specific middlewares), or all requests. If the middleware returns 404, the next middleware is called. Rails 2.3 only handles requests that fall to the bottom of the middleware stack.
Crosby’s point is, why bother with a chain of middlewares leading up to an application? All you need is a chain of middlewares that are the application. His example in the talk is an authentication middleware that sits on top of everything else, instead of the authentication layer being part of a monolithic application.
The best part is that middlewares can be created as black boxes, and simply included in your Rack application (or used as a middleware in your Rails 2.3 application). And it looks like the best tool for creating simple middlewares is Sinatra. This stuff isn’t even on the main Sinatra site yet, but you can read about it on the Sinatra blog. To quote: “multiple Sinatra applications can now be run in isolation and co-exist peacefully with other Rack based frameworks.” This is awesome – it means that a Sinatra application can be used as middleware for a Rails application (or other Sinatra applications). So it’s easy to build a big application by chaining together smaller Sinatra middlewares.
Since Ruby’s open source community is so strong, there’s no doubt we will be seeing pluggable middlewares that fit easily inside any Rack application. This is exciting stuff, and I look forward to building an app this way!