Method: Unicorn.builder

Defined in:
lib/unicorn.rb

.builder(ru, op) ⇒ Object

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.



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
84
85
86
87
88
89
90
91
92
93
# File 'lib/unicorn.rb', line 40

def self.builder(ru, op)
  # allow Configurator to parse cli switches embedded in the ru file
  op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)
  if ru =~ /\.ru$/ && !defined?(Rack::Builder)
    abort "rack and Rack::Builder must be available for processing #{ru}"
  end

  # always called after config file parsing, may be called after forking
  lambda do |_, server|
    inner_app = case ru
    when /\.ru$/
      raw = File.read(ru)
      raw.sub!(/^__END__\n.*/, '')
      eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
    else
      require ru
      Object.const_get(File.basename(ru, '.rb').capitalize)
    end

    if $DEBUG
      require 'pp'
      pp({ :inner_app => inner_app })
    end

    return inner_app unless server.default_middleware

    middleware = { # order matters
      ContentLength: nil,
      CommonLogger: [ $stderr ],
      ShowExceptions: nil,
      Lint: nil,
      TempfileReaper: nil,
    }

    # return value, matches rackup defaults based on env
    # Unicorn does not support persistent connections, but Rainbows!
    # does.  Users accustomed to the Rack::Server default
    # middlewares will need ContentLength middleware.
    case ENV["RACK_ENV"]
    when "development"
    when "deployment"
      middleware.delete(:ShowExceptions)
      middleware.delete(:Lint)
    else
      return inner_app
    end
    Rack::Builder.new do
      middleware.each do |m, args|
        use(Rack.const_get(m), *args) if Rack.const_defined?(m)
      end
      run inner_app
    end.to_app
  end
end