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). --- lib/unicorn.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'lib') diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 6dec03e..d3e9c3d 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -27,6 +27,44 @@ module Unicorn def run(app, options = {}) HttpServer.new(app, options).start.join end + + # This returns a lambda to pass in as the app, this does not "build" the + # app (which we defer based on the outcome of "preload_app" in the + # Unicorn config). The returned lambda will be called when it is + # time to build the app. + def builder(ru) + lambda do || + inner_app = case ru + when /\.ru$/ + raw = File.open(ru, "rb") { |fp| fp.sysread(fp.stat.size) } + raw.sub!(/^__END__\n.*/, '') + eval("Rack::Builder.new {(#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru) + else + require ru + Object.const_get(File.basename(ru, '.rb').capitalize) + end + + pp({ :inner_app => inner_app }) if $DEBUG + + # return value, matches rackup defaults based on env + 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 + end end # This is the process manager of Unicorn. This manages worker -- cgit v1.2.3-24-ge0c7