From 0b095ea72fb849682a1185a626eef247b5afc1cd Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 23 Mar 2009 02:03:31 -0700 Subject: unicorn_rails: support non-Rack versions of Rails This resurrects old code from Mongrel to wrap the Rails Dispatcher for older versions of Rails. It seems that Rails >= 2.2.0 support Rack, but only >=2.3 requires it. I'd like to support Rails 1.2.x for a while, too. --- bin/unicorn_rails | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'bin') diff --git a/bin/unicorn_rails b/bin/unicorn_rails index 177c109..fae6f4b 100755 --- a/bin/unicorn_rails +++ b/bin/unicorn_rails @@ -7,6 +7,7 @@ rails_pid = File.join(Unicorn::HttpServer::DEFAULT_START_CTX[:cwd], "/tmp/pids/unicorn.pid") cmd = File.basename($0) daemonize = false +static = true listeners = [] options = { :listeners => listeners } host, port = Unicorn::Const::DEFAULT_HOST, 3000 @@ -123,7 +124,22 @@ rails_loader = lambda do || when nil lambda do || require 'config/environment' - ActionController::Dispatcher.new + + # it seems Rails >=2.2 support Rack, but only >=2.3 requires it + old_rails = case ::Rails::VERSION::MAJOR + when 0, 1 then true + when 2 then Rails::VERSION::MINOR < 3 ? true : false + else + false + end + + if old_rails + require 'rack' + require 'unicorn/app/old_rails' + Unicorn::App::OldRails.new + else + ActionController::Dispatcher.new + end end when /\.ru$/ raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } @@ -147,12 +163,26 @@ app = lambda do || require 'active_support' require 'action_controller' ActionController::Base.relative_url_root = map_path if map_path + map_path ||= '/' + inner_app = inner_app.call Rack::Builder.new do - use Rails::Rack::LogTailer unless daemonize - use Rails::Rack::Debugger if $DEBUG - map(map_path || '/') do - use Rails::Rack::Static - run inner_app.call + if inner_app.class.to_s == "Unicorn::App::OldRails" + $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize + $stderr.puts "Debugger not available" if $DEBUG + map(map_path) do + if static + require 'unicorn/app/old_rails/static' + use Unicorn::App::OldRails::Static + end + run inner_app + end + else + use Rails::Rack::LogTailer unless daemonize + use Rails::Rack::Debugger if $DEBUG + map(map_path) do + use Rails::Rack::Static if static + run inner_app + end end end.to_app end -- cgit v1.2.3-24-ge0c7