Quick and dirty meshU recap

meshU, the workshop-oriented sister conference of mesh, took place this past Tuesday in Toronto. It sold out a few days beforehand, but with only four presentation slots and an estimated ~200 attendees, it had the feeling of a very small and tight-knit conference, big names aside.

Turning the tables

Avi Bryant kicked off the development track with a talk on relational database alternatives. Instead of getting too deep into the why, Avi focused on design considerations / best-practices when building an application on top of services like Amazon’s SDB, Google’s AppEngine, and Microsoft’s forthcoming SSDS (now in beta). He finished off with a first look at MagLev, a new Ruby VM (and Bryant / Gemstone joint), demonstrating two irb-like shells accessing the same global objects, complete with transactional support. Very curious stuff; more details on MagLev are to be revealed during Bryant’s upcoming RailsConf talk.

Managing great software teams

Afterwards I snuck into Reg Braithewaite’s talk on managing great software teams. I’ve been reading Raganwald for the better part of two years, and I’ve generally enjoyed reading his management-type articles, so I was looking forward to hearing him speak in person – even with Daniel Burka of Digg/Pownce fame presenting next door. Unsurprisingly, Reg is as a good a speaker as he is a blogger, and it felt like management and developer-types alike enjoyed his assortment of management anti-patterns, golden rules, and hindsights. You can catch his slides here, but despite being aesthetically pleasing, they’re hard to appreciate on their own.

Implementing OAuth

After lunch, my co-worker Taavi and I took in Leah Culver’s talk on OAuth, an open API authentication protocol. Her presentation was both introductory and yet very-technical, with fairly complex slides depicting a number of authentication interactions, complete with source code examples. With a dizzying number of token names and types, OAuth was perhaps the most complicated subject of the day, but Leah had a helpful hand; Flickr’s Cal Henderson, author and celebrity audience member, answered questions throughout (OAuth is largely based on the Flickr authentication model).

Thin and Rack

Last, but not least, Marc-Andre Cournoyer gave a development talk on Thin, a speedy event-driven web server for Ruby, and Rack, a web server interface and library. Marc-Andre coded up a couple of Rack/Thin examples while he presented, and I’m not sure what impressed me more: Thin, Rack, or Marc-Andre’s expert command of TextMate. It was a great presentation (with a hilarious introduction), so it’s a shame it was scheduled opposite Ryan Carson and John Resig, as the audience was minimal. Oh well; this subject would be better suited for a [Ruby|Rails]Conf, anyways.

So that wraps things up. From all the feedback I’ve heard, meshU was a pretty big success, and all indications point to a repeat event next year. Thanks again to all the volunteers, organizers and sponsors* for making it happen.

Disclosure note: FreshBooks is a sponsor of both Mesh Conference and meshU.

Stepping off the Rails with Rack, the modular web server interface for Ruby

What’s Rack?

Rack is a modular web server interface for Ruby. Instead of coding your application for a particular server interface like CGI or Ruby’s WEBrick, you adapt your application instead to the Rack interface specification, and invoke the appropriate server handler. With just a minuscule amount of work, you can easily deploy your app using any of WEBrick, Mongrel, FCGI or CGI.

You can install Rack as a Ruby gem:

gem install rack --include-dependencies

Simple Example

To get started with Rack, the entry point for your application must be an object that responds to a call method and takes a single parameter: a hash of environment variables. Your call method must return an array containing the HTTP response code, a set of headers including the content type, and finally the response body.

This code probably explains it best:

# hello_world.rb

require 'rubygems'
require 'rack'

class HelloWorld
  def call(env)

    [ 200, # HTTP Response Code
      { "Content-Type"=>"text/plain" }, # HTTP Headers
      [ "Hello, World!" ] # Body
    ]
  end
end

# Instantiate your app
hello_world_app = HelloWorld.new

# Deploy using WEBrick handler
Rack::Handler::WEBrick.run hello_world_app, :Port => 3000

You then start your web application simply by calling it:

# ruby hello_world.rb
[2007-06-17 20:42:31] INFO WEBrick 1.3.1
[2007-06-17 20:42:31] ruby 1.8.5 (2006-08-25) [i386-mswin32]
[2007-06-17 20:42:31] INFO WEBrick::HTTPServer#start: pid=5894 port: 3000

Even Simpler

Since most of us don’t want to deal with the raw request and response bodies ourselves, Rack supplies easy-to-use Request and Response classes. Here’s an example app that takes a single GET param:

...

class HelloWorld
  def call(env)
    req = Rack::Request.new(env)

    name = req.GET['name'] # post: req.POST['name']

    # Default content-type is text/html
    # Default status is 200
    Rack::Response.new.finish do |res|
       res.write "Hello, #{name}!" 
    end
  end
end

# This time, deploy using plain-old CGI through Apache/Lighttpd/etc.
hello_world_app = HelloWorld.new
Rack::Handler::CGI.run hello_world_app

Along these lines, Rack also has helpers for URL routing, cookie handling, and more.

Contact Form Example

For fun, I’ve taken this simple CGI contact form written in Ruby from my first Stepping off the Rails article and converted it to Rack. It can now be deployed using any of the handlers mentioned above, or still as plain CGI. Talk about flexibility.