Exploring Rack

If you're a Ruby programmer who has done any kind of web development, you've almost certainly used Rack, whether you know it or not, as it's the foundation which most Ruby web frameworks (Rails, Sinatra, etc.) are built upon. Let's dig into some of the basic concepts of Rack and even build a small app or two.


What Is Rack, Exactly?

Rack is several things, actually:

  • a web server interface
  • a protocol for building and composing web applications
  • a collection of middleware utilities

A Web Server Interface

Part of what's nice about Rack, is that it provides a standardized way for Ruby applications to talk to web servers, abstracting away the server stuff (listening on a port, accepting connections, parsing HTTP requests and responses, etc.) so that you can focus on what your application does.


The Protocol

The Rack protocol is very simple: a Rack application is simply a Ruby object with a call method. That method should accept an environment hash describing the incoming request and return a three-element array in the form of: [status, headers, body], where:

  • status is the HTTP status code.
  • headers is a hash of HTTP headers for the response.
  • body is the actual body of the response (e.g. the HTML you want to
    display). The body must also respond to each.

The easiest way to understand Rack's protocol, is by taking a look at some code.

First, get the rack gem and set up a directory:

Now create a file named config.ru and fill it in with the following:

Save the file, open up your terminal and run the rackup command:

The bottom few lines of output, in the above code, let you know that Rack is running your app at port 9292 using Ruby's built-in WEBrick web server. Point your browser to http://localhost:9292 to see a happy welcome message from Rack.

Kill the app (ctrl-c) and let's talk about what is going on here.

When you run the rackup command, Rack looks for a rackup config file (conventionally named config.ru, but you can name it whatever you want). It then starts a web server (WEBrick by default) and runs your app.

This protocol is the foundation on which popular frameworks like Rails and Sinatra are built. What they do is layer functionality like template rendering, route dispatching, managing database connections, content negotiation, etc. on top of this fundamental abstraction.

How they do this, is what brings us to the concept of middleware.


Middleware

Middleware gives you a way to compose Rack applications together.

A middleware (yes, it's both singular and plural) is simply a Rack application that gets initialized with another Rack application. You can define different middleware to do different jobs and then stack them together to do useful things.

For example, if you have a Rails app lying around (chances are, if you're a Ruby developer, that you do), you can cd into the app and run the command rake middleware to see what middleware Rails is using:

Every request that comes into this app starts at the top of this stack, bubbles its way down, hits the router at the bottom, which dispatches to a controller that generates some kind of response (usually some HTML), which then bubbles its way back up through the stack before being sent back to the browser.


A Middleware Example

Nothing fosters understanding a new concept like coding does, so let's build a very simple middleware that just converts the response body to uppercase. Open up our config.ru file from before and add the following:

Run the rackup command again and visit http://localhost:9292 to see our new middleware in action.

What Rack did here was build a Rack application that was the composition of the ToUpper and Hello applications. Internal to Rack, there's a Builder class that effectively constructed a new app by doing the equivalent of:

If there were more middleware present (like in the Rails stack), it would just nest them all the way down:


Request and Response Classes

When you start writing Rack applications and middleware, manipulating the [status, headers, body] array quickly becomes tedious.

Rack provides a couple of convenience classes, Rack::Request and Rack::Response, to make life a little bit easier.

Rack::Request wraps an env hash and provides you with convenience methods for pulling out the information you might need:

Rack::Response is complementary to Rack::Request, and gives you a more convenient way to construct a response. For example, our Hello app could be rewritten as follows:


Conclusion

In this article, we've covered the basic concepts of Rack, which should be enough for you to get a better understanding of what's under the hood of the many popular frameworks out there and also help you get your feet wet if you're interested in developing directly with Rack.

Code is an excellent teacher, and so if you're interested in Rack, I highly recommend looking at its source. It comes with a lot of very useful baked-in utilities and middleware (and plenty more at rack-contrib) that you can use and learn from.

Tags:

Comments

Related Articles