Lightweight Web Apps: Getting Started With Sinatra
For many reasons, I'm a huge fan of Rails for building web software, and by implication, coding in Ruby. When you've spent a little time working with Ruby, it's difficult to go back to more traditional languages such as PHP.
For less intense website or small applications, though, Rails can feel a bit heavy-handed. Whilst Rails 3 goes a long way to improving this, I've recently been trying out the lightweight ruby framework, Sinatra.
Sinatra is a barebones framework for getting web applications up and running quickly. The idea is that Sinatra gives you a base into which you pull together the things you need, effectively building your own mini-framework. The process is enhanced by the vast array of Ruby Gems that are available.
In this post, I'll show how to build a simple Sinatra application configured with Bundler and running on Rack that lets you quickly start building lightweight web applications.
Creating Your Sinatra Project
Unlike Rails, Sinatra doesn't come with generators, so you'll need to create the project layout yourself. Whilst you can use your own folder layout, there are a few conventions for which Sinatra is configured. Something like this should be good to get started:
mkdir -p myproject myproject/views myproject/public myproject/public/javascript myproject/public/css myproject/public/images
touch myproject/app.rb myproject/config.ru myproject/Gemfile```
Running these commands generates the following folder structure:
```textmyproject/
public/
public/css
public/images
public/javascript
views/
tmp/
app.rb
Gemfile
config.ru```
If you've used Rails 3, or worked with the Bundler gem and Rack, most of this should look familar: `Gemfile` is used by Bundler to declare rubygems the project relies on, whilst `config.ru` is used by Rack to configure and run the application.
### Bundling Gems
Next, edit the Gemfile to load Sinatra and any dependencies. My markup of choice for HAML for layout (HTML) and SASS for CSS stylesheets. Sinatra makes using HAML (or any other renderer, such as ERB) very simple:
```ruby# Gemfile
source :rubygems
gem "sinatra"
gem "haml"```
To install the bundle, just run `bundle install` from within your project folder:
```bashcd myproject
bundle install```
### Configure Sinatra to run on Rack
My development environment is setup to run Ruby projects on Rack using the Passenger gem. To configure Sinatra to run on Rack, our project just needs a `config.ru` file. This file tells Rack to load the any required gems and star your Sinatra application:
```ruby# Gemfile
require "rubygems"
require "bundler/setup"
require "sinatra"
require "haml"
require "app"
set :run, false
set :raise_errors, true
run Sinatra::Application```
### Build Your App
With the environment configured, it's time to build a small app to test everything. Start by adding a root path to `app.rb` and a corresponding `index.haml` file under `views/`:
```ruby# app.rb
set :haml, :format => :html5
get "/" do
haml :index
end```
```haml# views/index.haml
%html
%head
%title My Sinatra App
%body
%h1 Hello World
%p Your app is up and running!```
The `get` block routes any HTTP GET requests for the site's root path (`example.com/`) and renders `views/index.haml` using the HAML parser.
### Running Your Application
To see your application running, start Rack by calling `rackup` from your project directory.
```bashcd myproject
rackup```
Now navigate to [localhost:9292](http://localhost:9292) to see your Sinatra app working! All being well, you should be greeted with the following:
[![](https://assets.chrisblunt.com/2010/10/sinatra_app_running.jpeg "sinatra_app_running")](https://assets.chrisblunt.com/2010/10/sinatra_app_running.jpeg)
### Summary
Initially I had trouble seeing how Sinatra would be useful given the existence of full-stack frameworks like Rails. However, as I've taken the time to learn Sinatra, I can see the benefits of such a lightweight framework when used for certain tasks.
Whilst it shouldn't be thought of as a replacement for frameworks like Rails, Sinatra is another tool available to Ruby developers looking to quickly build and deploy small websites and applications.
#### When Should You Use Sinatra?
When you should use Sinatra over a larger framework like Rails is entirely dependent on the requirements of your site. Sinatra is capable of connecting with databases using gems like `ActiveRecord` or `DataMapper `in the same way Rails is. It's feasible, then, to build large-scale web applications with Sinatra - but my personal preference would be to stick with Rails.
The rule of thumb I'm using at the moment is that if my app requires a database, I'll build it in Rails, otherwise I'll try Sinatra.
With that in mind, I'm now using Sinatra to build and run lightweight websites such as the recently launched [portfolio of Mark Stocks](http://markstocks.com "Mark Stocks Photography"). Mark's site only requires simple server-side processing for handling contact forms - a full-blown Rails build seemed a be too much! Sinatra provided just the right base on which to build Mark's site, whilst retaining the flexibility that comes with the Ruby ecosystem.
### Next Steps
In the next tutorial, I'll extend the basic framework we've started here to include of a number of useful Ruby gems built for Sinatra, and show how to use your own custom helper code. When it's a little more polished, I'll also publish my working Sinatra base application on [github](http://github.com/cblunt).
### References
1. [The Sinatra Website](http://www.sinatrarb.com/)
2. [The Official Sinatra Book](http://sinatra-book.gittr.com/)
3. [Bare Sinatra App For Deploying To Passenger](http://www.nickhammond.com/2009/03/27/bare-sinatra-app-for-deploying-to-passenger/)
👋 Thanks for reading - I hope you enjoyed this post. If you find it helpful and want to support further writing and tutorials like this one, please consider supporting my work with a coffee!
Support ☕️