Skip to content

Instantly share code, notes, and snippets.

@rbmrclo
Forked from vangberg/README
Created August 23, 2013 04:44
Show Gist options
  • Save rbmrclo/6315624 to your computer and use it in GitHub Desktop.
Save rbmrclo/6315624 to your computer and use it in GitHub Desktop.
# 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
'config.ru' 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://gist.github.com/68277.git toodeloo
$ cd toodeloo
$ .. unpack gems and add to git repo ..
$ heroku create toodeloo
$ git remote add heroku [email protected]:toodeloo.git
$ git push heroku master
That's it. You can see it in action at http://toodeloo.heroku.com
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'))
end
# 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
end
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"))
DataMapper.auto_upgrade!
end
get '/' do
@todos = Todo.all
haml :index
end
post '/' do
Todo.create(:text => params['todo'])
redirect '/'
end
# Inspect the environment for additional information. This should *not* be
# accessible in a production app.
get '/env' do
content_type 'text/plain'
ENV.inspect
end
__END__
@@ index
!!!
%html
%head
%title Toodeloo
%body
%h1 Toodeloo
%ul
- @todos.each do |todo|
%li= todo.text
%form{:action => '/', :method => 'POST'}
%input{:type => 'text', :name => 'todo'}
%input{:type => 'submit', :name => 'Todo!'}
%a{:href => 'http://gist.github.com/68277'} Read more..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment