Deploying Rack-based Apps
Last updated 29 September 2015
Table of Contents
Heroku supports Rack and Rack-based web frameworks like Sinatra, Ramaze, and Camping.
If you have questions about Ruby on Heroku, consider discussing it in the Ruby on Heroku forums.
To run a Rack-based app, include a Gemfile, as well as a rackup file named config.ru in the app’s
root directory. The config.ru file convention has become common, so most
existing Rack applications should not require changes to deploy to Heroku.
Pure Rack apps
First, create a new directory and write a simple config.ru file:
$ mkdir hello $ cd hello $ cat > config.ru run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("Hello World!\n")] } [Ctrl-D] $ cat > Gemfile source 'https://rubygems.org' gem 'rack' [Ctrl-D]
Test it locally:
$ bundle install $ bundle exec rackup -p 9292 config.ru & $ curl http://localhost:9292 Hello World! $ kill %1
Deploy to Heroku:
$ git init $ git add . $ git commit -m 'pure rack app' $ heroku create $ git push heroku master
The app is now deployed to Heroku. Test by executing heroku open or by
visiting your app’s URL in your browser. You should see Hello, World!.
Frameworks
Sinatra
hello.rb:
require 'sinatra' get '/' do "Hello World!" end
config.ru:
require './hello' run Sinatra::Application
Gemfile:
source 'https://rubygems.org' gem 'sinatra'
Ramaze
hello.rb:
require 'ramaze' class MainController < Ramaze::Controller def index "Hello World!" end end
config.ru:
require ::File.expand_path('./../hello', __FILE__) Ramaze.start(:file => __FILE__, :started => true) run Ramaze
Gemfile:
source 'https://rubygems.org' gem 'ramaze'
Camping
Camping 2.0 does not require the Rack adapter; use run Hello instead.
hello.rb:
require 'camping' Camping.goes :Hello module Hello::Controllers class Index < R '/' def get render :hello end end end module Hello::Views def hello p "Hello World!" end end
config.ru:
require './hello' run Rack::Adapter::Camping.new(Hello)
Gemfile:
source 'https://rubygems.org' gem 'camping'
Database access
Using ActiveRecord
For non-Rails apps using ActiveRecord standalone, put this code into your application to access the DATABASE_URL:
require 'active_record' ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
The code above uses a default local PostgreSQL database named mydb, but you can change this value to point anywhere you like, or override by running your app with the DATABASE_URL environment variable set in your shell.
Using DataMapper or Sequel
DataMapper and Sequel both use database URLs natively, so configuration is a snap:
For DataMapper:
require 'data_mapper' DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
For Sequel:
require 'sequel' Sequel.connect(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')