I’m a big fan of Sinatra, a lightweight Ruby web framework, perfect for deploying chunks of code that do one thing, and one thing well. It perfectly espouses the philosophy of small things, loosely joined, making it really easy to build applications and services that expose a just few resources, perhaps sucking in and spitting out JSON or XML.
I have a few applications running Sinatra in the wild at the moment, all deployed using Passenger, through Rack. Getting them live on my VPS involves a bit of Capistrano configuration, a bit of poking Apache configurations, and configuring a database where required. I’ve streamlined this process somewhat, mostly through judicious use of copy and paste, but it still feels a bit of excessive for what are usually 50 line applications.
So, I was pleasantly surprised when I stumbled across Heroku again the other day, the startup aiming to provide an “instant ruby platform”. I tried them a while back when they offered an in-browser text editor, but really didn’t get on with that. Thankfully, they seem to have separated this component off into Heroku Garden, leaving the core service focused on providing a platform for instant deployment, accessed through a git repository.
I had a quick play, and within five minutes I had S3 FM up and running, with very little pain involved. And here’s how I did it.
It goes without saying that you need a Heroku account, and to have followed their procedure for adding your public SSH key and creating an empty application.
Heroku uses Rack, so you’ll need a Rack configuration (config.ru) file in your root folder. Mine looks somewhat like this:
require 'rubygems'
require 'sinatra'
disable :run
set :environment, :production
set :raise_errors, true
set :views, File.dirname(__FILE__) + '/views'
set :public, File.dirname(__FILE__) + '/public'
set :app_file, __FILE__
log = File.new("log/sinatra.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)
require 'radio.rb' # contains my application
run Sinatra.application
I added the heroku remote repository to git, and pushed to it. When I tried to load the site it fell over. I ran heroku console and the classes seemed to respond, but heroku logs threw back a stack trace that revealed the problem — the server was missing Haml.
So, I made a vendor folder, unpacked the Haml gem into it, required it at the top of the config.ru, committed and pushed again. And it worked. Perfect.
I’m impressed. I can see myself using Heroku for deploying a number of lightweight apps, and even extracting some frequently used chunks of code and turning them into web services.
Much as Github has encouraged people to share code by removing the weight of starting a project, perhaps Heroku will lower the barrier to providing small web services by removing the scary nature of starting a site.