Posts Tagged: Sinatra


25
Feb 10

Using Sinatra to serve smaller (than Rails) applications

So, maybe you’ve been coding Rails for a while and wondered – “do I really need this full stack framework to serve even small applications?”. Well, no you don’t :-)

Check out Norbauer’s DNS Tools (and on Github). It’s a small set of easy to use DNS tools. So small it would be overkill to serve it using Rails.

Norbauer’s using Sinatra to serve the app.

Start by installing the gem:

$ sudo gem install sinatra

After that it’s as simple as this:

# myapp.rb
require 'rubygems'
require 'sinatra'

get '/' do
  "Welcome to my homepage!"
end

get '/contact' do
  "My contact info is..."
end

At your terminal:

$ ruby myapp.rb
== Sinatra/0.9.4 has taken the stage on 4567 for development with backup from Mongrel

Now you can view it in your browser at http://localhost:4567 and http://localhost:4567/contact. Simple.

This is great for hosting apps from your command line (for example check out Taps) but what if you wanted to serve it like you would a Rails app?

Well, then you’d be using Rack.

Create a file named config.ru in the same folder as myapp.rb:

require 'myapp'
run Sinatra::Application

Now you can deploy your application as you would if it was written in Rails (thanks to config.ru).
If you want to check this setup at your localhost, run the following:

$ sudo gem install rack # make sure you have Rack installed
$ rackup # run the Rack application using config.ru

It’s as easy as that! Great for hosting small applications.

Also see:


Fork me on GitHub