Rack - the underlying machine of Ruby web applications

Moin Moin,

I you have ever wondered how a Ruby web applications/frameworks like Rails, Sinatra or Padrino work, you should understand what Rack is doing.

Rack was created 2007 by Christian Neukirchen (inital blog post). Rack does this:

"Rack aims to provide a minimal API for connecting web servers and web frameworks."

Pratik Naik wrote a nice thread with some basic examples. You should read it.

Actually I had problems installing Mongrel so I simply changed to Thin and used a Gemfile and an .rvmrc file for convenience. I put the code together in a mini project called airforec_one. Please note the typo in the name :).

.rvmrc

rvm use ruby-2.0.0-p353@airforec_one --create

Gemfile

source "https://rubygems.org"

gem 'rack'
gem 'thin'

init.rb

require 'rubygems'
require 'rack'
require 'thin'

class HelloMrPresident
  def call(env)
    [200, {"Content-Type" => "text/html"}, "Hey this is the president. Whazz up?"]
  end
end

Rack::Handler::Thin.run HelloMrPresident.new, :Port => 3000

Now run the code:

ruby init.rb
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

And open http://localhost:3000

What you see is the beginning of your career as a Ruby web framework developer. But just if Rails is too heavy, Sinatra is too less and Padrino is too young.

Cheers

Andy

Published: January 20 2014