Rack, the frameworks framework

RackWith all the Ruby frameworks popping up, we’re starting to see some similarities. All of them provide something new or unique but one part of their code is always the same. The part that plugs it into a web server. Ultimately, all web servers have to support all frameworks and vice-versa. That is a lot of duplicated code! That makes me yell, running in circles, waving my arms: not DRY, not DRY, not DRY!

What?

Rack (not the iRack) by Christian Neukirchen, solves this problem.

From Rack website:

Rack provides an minimal interface between webservers supporting Ruby and Ruby frameworks.

Rack looks like this in my head:
Rack

Handlers on the left (web servers) serve requests to Adapters (frameworks) on the right.
(Don’t ask me what the rabbit is doing there, must be important)

Run Forest!

But Rack can do a lot more then plug Ruby framework into webservers. It is a framework in itself.

When you install it:

sudo gem install rack

You get the rackup command. Which lets you start your app on any supported web server.


require rack/lobster

run Rack::Lobster.new

- config.ru

Start your app: rackup -p3000 then browse to http://localhost:3000, oh the cute lobster!

To run it on other web servers (mongrel by default) play with the -s option.

It’s a framework I said

The Handler API is only 1 method: call(env). Which allows you to use proc as application.


app = proc do |env|
  [200, { 'Content-Type' => 'text/html' }, 'no pepper plz!']
end
run app

- config.ru

That will return a 200 OK response with the text/html content type and the body no pepper plz!. env contains the request parameters, so you could play with the QUERY_STRING or wrap it inside a Rack::Request and get a request param value with request.params['name'].

Plug

You can use middlewares to filter the process. For example, you can add logging and detailed exception support with those 2 lines in your config.ru file:


use Rack::CommonLogger, STDOUT
use Rack::ShowExceptions

You can also validate your application, the requests and responses according to the Rack spec automatically using the Rack::Lint middleware.

(There’s also a Reloader, Static, Cascade, File and much more middlewares, check the doc)

You can also have fun with urls:


map /files do
  run Rack::File.new(.)
end

map /lobster do
  run Rack::Lobster.new
end

Check it out!

Rack is the best example of a very well design Ruby library. The code is simple, well separated and yet, easily extensible, it’s beautiful! I encourage you to check out the code but mostly to use it if you’re building any Ruby web framework (like we need other one!).

5 Responses to “Rack, the frameworks framework”


  1. 1 Philippe Rathe December 14, 2007 at 4:28 pm

    hmm… server …. deployment …

    eager to see what you guys have done.

    Dog…Rabbit Rack::Lobster … it’s the time we are going banana
    http://youtube.com/watch?v=szhJzX0UgDM

    :O)

  2. 2 macournoyer December 14, 2007 at 5:27 pm

    hey thx for the comment Philippe,

    Just to be clear, I’m not involved in the Rack project, it was created by Christian Neukirchen, I just though it was very cool.

    Lobsters are so cool, I know!

  3. 3 Darius Damalakas December 17, 2007 at 2:33 am

    I am .Net developer, but i’m reading blogs about Python, rails, java, and everything else. I noticed very quickly that the same ideas float everywhere, no matter what is the language syntax or platform used.
    All communities has all modern tools and concepts like rails-like web frameworks, ORM solutions, logging frameworks, etc.
    That shows a very healthy spread of idies, which is good ™ :)

  4. 4 macournoyer December 17, 2007 at 8:30 am

    Indeed this project was inspired by Paste written in Python.

    What other project did it remind you from?

  5. 5 melanie December 21, 2007 at 11:34 pm

    The rabbit? The dog is watching the rabbit; it’s a metaphor.

Leave a Reply




RefactorMyCode.comrefactorer Recommend Me

Top Clicks

  • None
Top Blogues page counter Add to Technorati Favorites

Blog Stats

  • 83,805 hits