1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
9 options
= { :listeners => listeners
}
10 host
, port
= Unicorn
::Const::DEFAULT_HOST, Unicorn
::Const::DEFAULT_PORT
12 ENV['RAILS_ENV'] ||= "development"
14 opts
= OptionParser
.new("", 24, ' ') do |opts
|
15 cmd
= File
.basename($0)
16 opts
.banner
= "Usage: #{cmd} " \
17 "[ruby options] [#{cmd} options] [rackup config file]"
18 opts
.separator
"Ruby options:"
21 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
22 eval line
, TOPLEVEL_BINDING
, "-e", lineno
26 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
30 opts
.on("-w", "--warn", "turn warnings on for your script") do
34 opts
.on("-I", "--include PATH",
35 "specify $LOAD_PATH (may be used more than once)") do |path
|
36 $LOAD_PATH.unshift(*path
.split(/:/))
39 opts
.on("-r", "--require LIBRARY",
40 "require the library, before executing your script") do |library
|
44 opts
.separator
"#{cmd} options:"
46 # some of these switches exist for rackup command-line compatibility,
48 opts
.on("-o", "--host HOST",
49 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
54 opts
.on("-p", "--port PORT", "use PORT (default: #{port})") do |p
|
59 opts
.on("-E", "--env RAILS_ENV",
60 "use RAILS_ENV for defaults (default: development)") do |e
|
64 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
65 daemonize
= d
? true : false
68 # Unicorn-specific stuff
69 opts
.on("-l", "--listen {HOST:PORT|PATH}",
70 "listen on HOST:PORT or PATH",
71 "this may be specified multiple times",
72 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
76 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
77 options
[:config_file] = f
80 opts
.on("-P PATH", "DEPRECATED") do |v
|
81 warn
%q{Use of -P is ambiguous and discouraged}
82 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
83 ENV['RAILS_RELATIVE_URL_ROOT'] = v
86 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
87 "(default: /)") do |v|
88 ENV['RAILS_RELATIVE_URL_ROOT'] = v
91 # I'm avoiding Unicorn-specific config options on the command-line.
92 # IMNSHO, config options on the command-line are redundant given
93 # config files and make things unnecessarily complicated with multiple
94 # places to look for a config option.
96 opts.separator "Common options:"
98 opts.on_tail("-h", "--help", "Show this message") do
99 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
103 opts.on_tail("-v", "--version", "Show version") do
104 puts " v#{Unicorn::Const::UNICORN_VERSION}"
111 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
113 if config && config =~ /\.ru\z/
114 # parse embedded command-line options in config.ru comments
115 /^#\\(.*)/ =~ File.read(config) and opts.parse!($1.split(/\s+/))
118 def rails_builder(config, daemonize)
119 # this lambda won't run until after forking if preload_app is false
121 # Load Rails and (possibly) the private version of Rack it bundles.
123 require 'config/boot'
124 rescue LoadError => err
125 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
128 inner_app = case config
130 require 'config/environment'
132 defined?(::Rails::VERSION::STRING) or
133 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
134 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
135 old_rails = case ::Rails::VERSION::MAJOR
137 when 2 then Rails::VERSION::MINOR < 3 ? true : false
143 require 'unicorn/app/old_rails'
144 Unicorn::App::OldRails.new
146 ActionController::Dispatcher.new
149 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
150 raw.sub!(/^__END__\n.*/, '')
151 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
154 Object.const_get(File.basename(config, '.rb').capitalize)
158 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
159 if inner_app.class.to_s == "Unicorn::App::OldRails"
161 # patches + tests welcome, but I really cbf to deal with this
162 # since all apps I've ever dealt with just use "/" ...
163 $stderr.puts "relative URL roots may not work for older Rails"
165 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
166 $stderr.puts "Debugger not available" if $DEBUG
168 use Unicorn::App::OldRails::Static
172 use Rails::Rack::LogTailer unless daemonize
173 use Rails::Rack::Debugger if $DEBUG
175 use Rails::Rack::Static
183 app = rails_builder(config, daemonize)
184 listeners << "#{host}:#{port}" if set_listener
189 :unicorn_options => options,
191 :daemonize => daemonize,
195 # ensure Rails standard tmp paths exist
196 options[:after_reload] = lambda do
197 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
201 options[:pid] = "tmp/pids/unicorn.pid"
202 Unicorn::Launcher.daemonize!(options)
204 Unicorn.run(app, options)