Robust way to deploy a Rack application (Sinatra)
Nginx / Unicorn FTW!
Nginx in front to serve static files and unicorn to handle Sinatra app.
Benefits: Performance, good load balancing with unix socks and deploy/upgrade without any downtimes (you can upgrade Ruby/Nginx/Sinatra/app without downtime).
How-to : http://sirupsen.com/setting-up-unicorn-with-nginx/ .
How to ship a Sinatra application as a gem and deploy it?
Ignoring path-issues for now, here is how I solved it:
The application bundled as gem, tubemp
.
A Gemfile
to install and include the gemified application:
gem 'tubemp'
A config.ru
which runs the gemified application, called Tubemp
:require 'rubygems'
require 'bundler/setup' # To allow inclusion via the Bundle/Gemfile
require 'sinatra' # Sinatra is required so we can call its "set"
require 'tubemp' # And include the application
# Set the environment to :production on production
set :environment, ENV['RACK_ENV'].to_sym
# And fire the application.
run Tubemp
These two files is all that is needed to make a new rack-application, which includes thegemified Sinatra-app and as such are bootable trough e.g. nginx,
passenger or simply
rackup
. Continuously run Sinatra on Apache server
This is usually due to the shell (for example bash) exiting and sending hangup or kill signals to all its child processes. To start the server in the background and shield it from the HUP signal you can use the command nohup
:
nohup - run a command immune to hangups, with output to a non-tty
For example:nohup ruby sinatra_app.rb &
http://en.wikipedia.org/wiki/NohupFor more robust deployment options you might want to look at something like Nginx and/or Phusion Passenger:
Robust way to deploy a Rack application (Sinatra)
How to create rack application to be used by Rails?
I got my question answered in another question:
How to read POST data in rack request
require 'json'
class Greeter
def call(env)
req = Rack::Request.new(env)
if req.post?
puts req.POST()
end
[200, {"Content-Type" => "application/json"}, [{x:"Hello World!"}.to_json]]
end
end
run Greeter.new
and use JSON.parse( req.body.read )
to parse POST data. What's the difference between a Rack app and a Sinatra app?
I think the asker wants the merits of keeping it to just Rack.
Considering Sinatra is already an extremely thin & minimalistic veneer for typical web applications, my short answer will be: when your needs are atypical and/or spartan
when you don't even need...
- layout/ templating - you enjoy writing out raw string http content
- cookies and sessions - no memory, everything is one-shot default
- methods - your app abuses the URL and HTTP VERB in crazy ass ways
- to be modern - you have nostalgia with CGI/perl scripts in the 90s
- any structure - your app returns time of day in text. per se.
You gain really little except code lines, and a smarmy twinkle in your eye when impressing geeks
honest.
p.s.: I maintain a nice skeleton for my own needs at http://github.com/codepants/yasumi
Sinatra/Rack Sleep until Response Ready (like Exchange ActiveSync)
You could also check out macournoyer's http://github.com/macournoyer/pusher "The Rack App that pushes" and collin's http://github.com/collin/orbited-ruby/
Related Topics
Truncate String When It Is Too Long
Is It a Bad Practice to Randomly-Generate Test Data
Ruby Linkify for Urls in Strings
Adding Bootstrap Icon to Button in Ruby on Rails
When Is The Enumerator::Yielder#Yield Method Useful
Ruby Datamapper Table Inheritance with Associations
Rack: How to Store The Url as a Variable
Broken Rails Routes After Implementing Single Table Inheritance
Difference Between Render and Yield in Rails
Statically Compile Pdftk for Heroku. Need to Split PDF into Single Page Files
How to Generate The Form for an Existing Model in Rails
How to Remove Header and Footer from Some of The Pages in Ruby on Rails
Starting with Redmine Locally - How Easy Is Migration to Server Later
How to "Break" Out of a Case...While in Ruby
How to Find Word with The Greatest Number of Repeated Letters