From 3e80ccb60e2b3632916094ac436806ab1cf03b11 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 18 Feb 2010 19:11:50 -0800 Subject: Unicorn::builder to wrap our Rack::Builder usage This should make it easier to reuse code in derivative servers like Rainbows! and Zbatery. Unfortunately, we can't depend on Rack::Builder/Rack::Server yet since Rack 1.1 just got them and notable frameworks (like Rails 2.3.x) do not fully work with Rack 1.1 yet). This also fixes subtle issue with config.ru files that could have variables that conflict with the Unicorn-specific namespace (this bug still affects "unicorn_rails", which could use some reworking as well). --- bin/unicorn | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) (limited to 'bin') diff --git a/bin/unicorn b/bin/unicorn index 5af021d..0da0869 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -108,49 +108,19 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.parse! ARGV end -config = ARGV[0] || "config.ru" -abort "configuration file #{config} not found" unless File.exist?(config) +ru = ARGV[0] || "config.ru" +abort "configuration file #{ru} not found" unless File.exist?(ru) -if config =~ /\.ru$/ +if ru =~ /\.ru$/ # parse embedded command-line options in config.ru comments - if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/ + if File.open(ru, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/ opts.parse! $1.split(/\s+/) end end require 'pp' if $DEBUG -app = lambda do || - # require Rack as late as possible in case $LOAD_PATH is modified - # in config.ru or command-line - inner_app = case config - when /\.ru$/ - raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } - raw.sub!(/^__END__\n.*/, '') - eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) - else - require config - Object.const_get(File.basename(config, '.rb').capitalize) - end - pp({ :inner_app => inner_app }) if $DEBUG - case ENV["RACK_ENV"] - when "development" - Rack::Builder.new do - use Rack::CommonLogger, $stderr - use Rack::ShowExceptions - use Rack::Lint - run inner_app - end.to_app - when "deployment" - Rack::Builder.new do - use Rack::CommonLogger, $stderr - run inner_app - end.to_app - else - inner_app - end -end - +app = Unicorn.builder(ru) listeners << "#{host}:#{port}" if set_listener if $DEBUG -- cgit v1.2.3-24-ge0c7