Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rails < 2.3 on Rack

View config.ru
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
# Because Unicorn is such a brilliant piece of software, it wraps older,
# non-Rack versions of Rails in a Rack handler. That way Unicorn
# itself can target Rack and not have to worry about monkey patching
# Rails' dispatcher.
#
# This means we can do the same, and even more.
#
# Starting Rackhub locally:
#
# Thin:
# $ thin -R config.ru start
#
# Unicorn:
# $ unicorn_rails -c config/unicorn.rb
#
# JRuby:
# $ sudo env RAILS_ENV=fi jruby -S rackup -s mongrel -p 80
 
# Load our Rails app and Rack wrapper
require 'config/environment'
require 'unicorn/app/old_rails'
require 'unicorn/oob_gc'
 
module Rails
module Rack
# We're telling Unicorn we're not an old version of Rails, even
# though we are. As such, if we're running in the foreground
# Unicorn will want LogTailer. So we hand it a stub.
class LogTailer
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
 
if RAILS_ENV == 'development'
# If you're not using Nginx or something else to serve static
# assets locally, Unicorn will try to use a Rack middleware to
# do it. Again: we're old Rails pretending to be new Rails, so we
# lie slightly.
require 'unicorn/app/old_rails/static'
Static = Unicorn::App::OldRails::Static
else
# we never want to use static in non-development environments, since nginx
# handles this for us.
class Static
def initialize(app)
@app = app
end
def call(env)
@app.call(env)
end
end
end
end
end
 
# Out-of-band GC, runs GC after every 10th request and after the response
# has been delivered.
use Unicorn::OobGC, interval=10
 
# Testing, testing.
map '/__rack__' do
run proc { [ 200, { 'Content-Type' => 'text/plain' }, "Rails on Rack!" ] }
end
 
# Where the magic happens, so to speak.
map '/' do
if RAILS_ENV != 'production'
begin
require 'rack/bug'
use Rack::Bug, :password => nil
rescue LoadError
end
 
use Rails::Rack::Static
end
 
run Unicorn::App::OldRails.new
end
GC.start

without GC.disable is meaningless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.