Skip to content

Instantly share code, notes, and snippets.

@eegrok
Forked from vangberg/README
Created March 29, 2012 00:31
Show Gist options
  • Save eegrok/2231844 to your computer and use it in GitHub Desktop.
Save eegrok/2231844 to your computer and use it in GitHub Desktop.
Deploying a Sinatra app with database to Heroku
# Deploying a Sinatra app with database to Heroku
# note this is a fork of someone elses work, I just updated it to work with latest stuff
## 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/2231844.git toodeloo
$ cd toodeloo
$ heroku create footestapp-2333
$ bundle install
$ git add Gemfile.lock
$ git commit -m "add Gemfile.lock"
$ git push heroku master
That's it. You can see it in action at http://footestapp-2333.heroku.com
require './toodeloo'
run Sinatra::Application
source 'http://rubygems.org'
gem 'rack'
gem 'sinatra'
gem 'dm-core'
gem 'haml'
gem 'dm-postgres-adapter'
gem 'dm-migrations'
gem 'pg'
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
# 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, Serial
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 whether we're in production
# / not.
DataMapper.setup(:default, (ENV["HEROKU_POSTGRESQL_ORANGE_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