ichverstehe (owner)

Forks

Revisions

gist: 68277 Download_button fork
public
Description:
Deploying a Sinatra app to Heroku
Public Clone URL: git://gist.github.com/68277.git
Embed All Files: show embed
README #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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 git@heroku.com:toodeloo.git
$ git push heroku master
 
That's it. You can see it in action at http://toodeloo.heroku.com
 
config.ru #
1
2
3
4
require 'toodeloo'
 
run Sinatra::Application
 
toodeloo.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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..