Skip to content

Commit 5985dd5

Browse files
jeremyevansEric Wong
authored andcommitted
Support default_middleware configuration option
This allows for the equivalent of the -N/--no-default_middleware command line option to be specified in the configuration file so it doesn't need to be specified on the command line every time unicorn is executed. It explicitly excludes the use of -N/--no-default_middleware as an embedded configuration option in the rackup file, by ignoring the options after ARGV is parsed. In order to allow the configuration method to work, have the lambda that Unicorn.builder returns accept two arguments. Technically, only one argument is needed for the HttpServer instance, but I'm guessing if the lambda accepts a single argument, we expect that to be a rack application instead of a lambda that returns a rack application. The command line option option to disable default middleware will take precedence over the unicorn configuration file option if both are present. For backwards compatibility, if the lambda passed to HttpServer accepts 0 arguments, then call it without arguments. [ew: fix precedence for arity checking in build_app! configurator: ensure -N is respected when set in command-line]
1 parent c93a392 commit 5985dd5

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

bin/unicorn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require 'optparse'
66
ENV["RACK_ENV"] ||= "development"
77
rackup_opts = Unicorn::Configurator::RACKUP
88
options = rackup_opts[:options]
9+
set_no_default_middleware = true
910

1011
op = OptionParser.new("", 24, ' ') do |opts|
1112
cmd = File.basename($0)
@@ -60,7 +61,7 @@ op = OptionParser.new("", 24, ' ') do |opts|
6061

6162
opts.on("-N", "--no-default-middleware",
6263
"do not load middleware implied by RACK_ENV") do |e|
63-
rackup_opts[:no_default_middleware] = true
64+
rackup_opts[:no_default_middleware] = true if set_no_default_middleware
6465
end
6566

6667
opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
@@ -110,6 +111,7 @@ op = OptionParser.new("", 24, ' ') do |opts|
110111
opts.parse! ARGV
111112
end
112113

114+
set_no_default_middleware = false
113115
app = Unicorn.builder(ARGV[0] || 'config.ru', op)
114116
op = nil
115117

lib/unicorn.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ def self.builder(ru, op)
4545
abort "rack and Rack::Builder must be available for processing #{ru}"
4646
end
4747

48-
# Op is going to get cleared before the returned lambda is called, so
49-
# save this value so that it's still there when we need it:
50-
no_default_middleware = op[:no_default_middleware]
51-
5248
# always called after config file parsing, may be called after forking
53-
lambda do ||
49+
lambda do |_, server|
5450
inner_app = case ru
5551
when /\.ru$/
5652
raw = File.read(ru)
@@ -66,7 +62,7 @@ def self.builder(ru, op)
6662
pp({ :inner_app => inner_app })
6763
end
6864

69-
return inner_app if no_default_middleware
65+
return inner_app unless server.default_middleware
7066

7167
middleware = { # order matters
7268
ContentLength: nil,

lib/unicorn/configurator.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def reload(merge_defaults = true) #:nodoc:
8888
RACKUP[:set_listener] and
8989
set[:listeners] << "#{RACKUP[:host]}:#{RACKUP[:port]}"
9090

91+
RACKUP[:no_default_middleware] and
92+
set[:default_middleware] = false
93+
9194
# unicorn_rails creates dirs here after working_directory is bound
9295
after_reload.call if after_reload
9396

@@ -265,6 +268,14 @@ def worker_processes(nr)
265268
set_int(:worker_processes, nr, 1)
266269
end
267270

271+
# sets whether to add default middleware in the development and
272+
# deployment RACK_ENVs.
273+
#
274+
# default_middleware is only available in unicorn 5.5.0+
275+
def default_middleware(bool)
276+
set_bool(:default_middleware, bool)
277+
end
278+
268279
# sets listeners to the given +addresses+, replacing or augmenting the
269280
# current set. This is for the global listener pool shared by all
270281
# worker processes. For per-worker listeners, see the after_fork example

lib/unicorn/http_server.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Unicorn::HttpServer
1414
attr_accessor :app, :timeout, :worker_processes,
1515
:before_fork, :after_fork, :before_exec,
1616
:listener_opts, :preload_app,
17-
:orig_app, :config, :ready_pipe, :user
17+
:orig_app, :config, :ready_pipe, :user,
18+
:default_middleware
1819
attr_writer :after_worker_exit, :after_worker_ready, :worker_exec
1920

2021
attr_reader :pid, :logger
@@ -70,6 +71,7 @@ def initialize(app, options = {})
7071
@app = app
7172
@request = Unicorn::HttpRequest.new
7273
@reexec_pid = 0
74+
@default_middleware = true
7375
options = options.dup
7476
@ready_pipe = options.delete(:ready_pipe)
7577
@init_listeners = options[:listeners] ? options[:listeners].dup : []
@@ -784,12 +786,12 @@ def listener_names(listeners = LISTENERS)
784786
end
785787

786788
def build_app!
787-
if app.respond_to?(:arity) && app.arity == 0
789+
if app.respond_to?(:arity) && (app.arity == 0 || app.arity == 2)
788790
if defined?(Gem) && Gem.respond_to?(:refresh)
789791
logger.info "Refreshing Gem list"
790792
Gem.refresh
791793
end
792-
self.app = app.call
794+
self.app = app.arity == 0 ? app.call : app.call(nil, self)
793795
end
794796
end
795797

0 commit comments

Comments
 (0)