Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2009 01:06
What would you like to do?
Deploying a Sinatra app to Heroku
# Deploying a Sinatra app to Heroku
## Database
The location of the database Heroku provides can be found in the environment
variable DATABASE_URL. Check the configure-block of toodeloo.rb for an example
on how to use this.
## Server
Heroku is serving your apps with thin, with means you have all your thin goodness available,
such as EventMachine.
## Rackup file
Heroku can serve all Rack applications. It looks for a rackup file named
'' in the root directory. Thus serving a Sinatra app is simple:
require 'toodeloo'
run Sinatra::Application
## Create app and deploy
The whole process of deploying this small Sinatra app was as follows:
$ git clone git:// toodeloo
$ cd toodeloo
$ .. unpack gems and add to git repo ..
$ heroku create toodeloo
$ git remote add heroku
$ git push heroku master
That's it. You can see it in action at
require 'toodeloo'
run Sinatra::Application
require 'sinatra'
require 'dm-core'
# Heroku has a limited number of gems installed, and chance is that you need
# some additional gems, like haml. The trick is to vendor them with your app.
# First, add this snippet, which will add vendor/*/lib to your load path:
Dir['vendor/*'].each do |lib|
$:.unshift(File.join(File.dirname(__FILE__), lib, 'lib'))
# Next, unpack all the gems you need in vendor:
# $ mkdir vendor
# $ cd vendor/
# $ gem unpack haml
# And finally require it (which isn't really necessary in this case, since
# Sinatra does it for you, but for sake of the example we'll do it):
require 'haml'
# Make sure your DataMapper models are defined *before* the configure
# block, otherwise your DB won't be updated and you're in for trouble and
# what-not.
class Todo
include DataMapper::Resource
property :id, Integer, :serial => true
property :text, String
configure do
# Heroku has some valuable information in the environment variables.
# DATABASE_URL is a complete URL for the Postgres database that Heroku
# provides for you, something like: postgres://user:password@host/db, which
# is what DM wants. This is also a convenient check wether we're in production
# / not.
DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{Dir.pwd}/development.sqlite3"))
get '/' do
@todos = Todo.all
haml :index
post '/' do
Todo.create(:text => params['todo'])
redirect '/'
# Inspect the environment for additional information. This should *not* be
# accessible in a production app.
get '/env' do
content_type 'text/plain'
@@ index
%title Toodeloo
%h1 Toodeloo
- @todos.each do |todo|
%li= todo.text
%form{:action => '/', :method => 'POST'}
%input{:type => 'text', :name => 'todo'}
%input{:type => 'submit', :name => 'Todo!'}
%a{:href => ''} Read more..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment