I’ve been working on a handful of Sinatra applications recently and I’ve deployed them all using Rack on Passenger. While Sinatra only does require one file to run on your box with mongrel, when it’s using rack with passenger it requires a few more directories and files. It’s pretty much your basic rack directory structure but it’s nice to have a bare app to work. I went ahead and created a bare sinatra application and threw it up on github to make the process faster of creating a new sinatra application.
Here’s the details of the sinatra skeleton application
.gitignore - ignore .swp and other temp files
*~
*.swp
config/db.rb
Capfile - this is needed for capistrano to deploy. This is put here by running capify . in the directory of the application
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'
app.rb - The core app file. The file just includes a require to rubygems and sinatra which is all that is needed and a route or two.
require 'rubygems'
require 'sinatra'
config.ru - this is an important file since this is what rack looks at to figure out how to run your application properly.
require 'rubygems'
require 'sinatra'
require 'app.rb'
path = '' # set the root path of your app here. e.g. /var/www/username/somesite
set :root, path
set :views, path + '/views'
set :public, path + '/public'
set :run, false # this line tells mongrel not to run and to let passenger handle the application
set :environment, :production
set :raise_errors, true
# sinatra doesn't have anything built in for logging so you can use the stdout to log to a file
log = File.new("sinatra.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)
# always put this line last so that all of your settings are properly loaded before sinatra is booted up
run Sinatra::Application
config/deploy.rb - contains your deploy recipe. I usually put a database connection file in here as well such as db.rb. When I need activerecord then I just put a require activerecord in my app.rb file and then a load config/db.rb to get my models connected to my database.
config/db.rb
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:username => 'root',
:password => '',
:host => 'localhost',
:database => 'somedb'
)
models.rb - I usually put all of my models in here just to organize the code better. Again, this isn’t necessary for sinatra, it’s all up to however you would like to do it.
class Book < < ActiveRecord::Base
validates_presence_of :title
end
public/images
public/stylesheets
public/javascripts
every application is going to have those three directories, might as well create them by default.
tmp/
in order to restart your sinatra application with passenger you need the tmp directory so that you can run touch tmp/restart.txt to restart your application.
views/
views contains all of my sinatra layout and action files. Sinatra uses layout.haml/erb as your default layout similar to how rails uses the application layout file. Put your sass files in here as well and create a path in your app.rb similar to the following to get your sass to compile to css.
get '/stylesheets/application.sass' do
sass :application
end