1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

Riding Shotgun with Sinatra


Now that you can write controller logic and views and Haml, you can start putting together some more complex application. There's one very annoying thing though: Sinatra does not reload its code whenever you make a change. If you make a change, you have to stop and restart the server. This is just an extra step and generally an annoyance. This where shotgun comes in.

Shotgun sits between Sinatra and Rack. Shotgun will reload your application every time a new request comes in from the web browser. This is not exactly the most efficient way of doing things, but it's great for development. In essence, this is a "development mode" as in Rails applications. This could have been included with Sinatra, but the code required to do this is beyond the scope of the Sinatra project. Instead of adding bloat many people wouldn't use, Sinatra developer will just have to use Shotgun.

Installing Shotgun

Shotgun is a simple gem. It requires only on a few small Ruby-only gems, so there's no third party software or native compilation. It's installed as any other gem.

$ sudo gem install shotgun

Using Shotgun

The shotgun gem also installed a shotgun command. As an argument, pass the filename of your Sinatra application. To make this a little more Sinatra-like, we're also going to add another switch to change the port to 4567, the default port Sinatra uses.

So here's the little test application. It's nothing special, it just prints out a message. Note there are no changes at all to your Sinatra application, only how it's launched.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

get '/' do
  "This is a test message"
$ shotgun -p 4567 shotgun_test.rb
== Shotgun starting Rack::Handler::Mongrel on localhost:4567

Now, without shutting the server down, make any changes you wish to your Sinatra application. They'll be reflected automatically in any future requests you make to the server. It's like magic!

Other Shotgun Options

There are a few other options to Shotgun you might want to be aware of. Most of these won't be of use to the average user, but you never know when you might need them.

  • -s, --server SERVER

    This defines which web server you want to use. It will default to mongrel, but falls back to webrick. This is passed to Rack, and defines which Rack server to use.

  • -o, --host HOST

    Which hostname to bind to. This will bind to by default, meaning anyone on localhost, your LAN, or that can reach you over the Internet can access your running Sinatra apps. If someone might be snooping around or you just don't want people stumbling on your development server, set this option to localhost.

  • -d, --debug

    Output extra debug information. This will be very useful when something goes wrong.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Sinatra
  6. Riding Shotgun with Sinatra

©2014 About.com. All rights reserved.