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!).

About these ads

17 Comments

Filed under rails, ruby, tutorial

17 responses to “Rack, the frameworks framework

  1. hmm… server …. deployment …

    eager to see what you guys have done.

    Dog…Rabbit Rack::Lobster … it’s the time we are going banana

    :O)

  2. 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. Darius Damalakas

    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. Indeed this project was inspired by Paste written in Python.

    What other project did it remind you from?

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

  6. Pingback: decodeURI » Creating a Rack Middleware for Minifying Your Javascript files

  7. TJ

    Its not beautiful on the inside haha have you looked at the source? VERY messy

  8. Pingback: James Robertson (jrbookmarks) 's status on Thursday, 03-Sep-09 16:17:41 UTC - Identi.ca

  9. Pingback: James Robertson (jrbookmarks) 's status on Thursday, 03-Sep-09 16:18:27 UTC - Identi.ca

  10. There’s a Rack coding contest starting now if you want to enter. It’s at http://www.coderack.org

    Some great prizes and it promises to be a lot of fun.

  11. Pingback: Creating a Rack Middleware for Minifying Your Javascript files « decodeURI

  12. Pingback: Rack - What the hell?

  13. Great Submit, Many thanks

  14. Witty! I’m bookmarking you site for future use.

  15. I’ve just been promoted to Level 162 in #MobsterWorld. Beat me in the game! http://t.co/DL3UwuhH

  16. Pingback: Gaptek

  17. magnificent post, very informative. I’m wondering why the opposite experts of this sector do not understand this. You must continue your writing. I am confident, you have a huge readers’ base
    already!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s