Fork me on GitHub

random thoughts and rumblings

by csspixel ( contact me via email )

Web Development With Sinatra

A lot of people jumped on the Rails band wagon and started using Ruby with it. I was never fan of large frameworks that force you to do things certain way. I love to have freedom to do things my way, even if it is not the best way. It is more fun for sure :-).

Thats how I found Sinatra, a DSL for creating web apps in Ruby.

This intro page shows really nice quick examples to get you going. There is also a book with more detailed instructions. Promising blog with tutorials for beginners. Get inspired before you dive in.

So instead of telling you how to create yet another hello world web app :-), I would like to touch on a few things that were not clear to me when I started using Sinatra and Ruby.

Setting up project structure

Sinatra based web app fits in a single file. This is great for a quick hello world app. For anything more serious, I like to create the following folder structure:

To get everything loaded properly for the above example, write something like this in your app.rb:

require 'rubygems'
require 'sinatra'
require 'helpers/sinatra'
require 'config/database'
require 'model/somemodel'

This is not set in stone. For example, you can stick all your ORM models in the main file, but things might get messy later on.

The tmp folder is used to reload your app once in production if using Passenger. Simply create an empty restart.txt file inside of it and refresh your app in the browser.

For the views I use erb templates as I like to write html. It looks like a lot of people prefer Haml and Sass. Check out both, you might like them.

I prefer DataMapper as ORM for my Sinatra based apps. There is also Sequel. You can also use ActiveRecord, especially if you are familiar with Rails. For a key-value DB I would recommend Redis. Very small, damn fast and quite fun to work with.

If you are using TextMate, check out Sparkup bundle to speed up your html coding.

Use Rerun when developing locally

At the beginning I was always using Shotgun gem to automatically reload the app when making changes. The problem is that Shotgun reloads your app on every request (it forks your app, kills it then loads it again). This takes a long time and your web app feels really slow. Instead I use Rerun.

To install:

sudo gem install rerun

To use:

rerun ruby app.rb

It monitors the file system and when there are changes, it restarts it. Works great!

Deployment

Please stay away from crappy shared web hosts. If you are short on money, use Heroku. They are great and offer a free service with limited resources. More than plenty for your toy apps while learning Sinatra.

Instructions to deploy Sinatra on Heroku.

If you have $20/month to spend on hosting, and feel like setting up everything on your own, get a VPS from either Slicehost or Linode. They are both great companies with no bullshit attached. I am personally running Ubuntu server with Apache and Phusion Passenger to run my Sinatra apps. Passenger works for Apache and Nginx, your choice. Keep in mind that Nginx is much lighter and usually better choice for a small VPS.

To setup Passenger on ubuntu/debian add following repo to your software sources:

deb http://apt.brightbox.net hardy main

For Apache:

apt-get install libapache2-mod-passenger

For Nginx

apt-get install nginx-brightbox

Once you push your Sinatra app to your VPS either via git, scp or whatever method you like, create a config.ru file inside your app folder with the following:

require 'app'
run Sinatra::Application


Change app with the name of your Sinatra app source file. Now simply point your web server document root to the folder of your app and you are good to go.

Authentication

For a simple http auth, check out the Sinatra book. If you need an authentication that is backed by a database, I would recommend to check out this code. It is a very simple DataMapper based authentication. It should give you a good idea how to get things going. For a third party authentication look at this post.

Where to get help

If you would like to post a question, check out Google groups.
For a quick help over IRC there is #sinatra channel at Freenode. It is not that active though.

Mar 05, 2010 - comments

Older Posts

Interesting Evenets