2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
7 rails_pid
= "#{Unicorn::HttpServer::START_CTX[:cwd]}/tmp/pids/unicorn.pid"
8 cmd
= File
.basename($0)
11 options
= { :listeners => listeners
}
12 host
, port
= Unicorn
::Const::DEFAULT_HOST, Unicorn
::Const::DEFAULT_PORT
14 ENV['RAILS_ENV'] ||= "development"
16 opts
= OptionParser
.new("", 24, ' ') do |opts
|
17 opts
.banner
= "Usage: #{cmd} " \
18 "[ruby options] [#{cmd} options] [rackup config file]"
19 opts
.separator
"Ruby options:"
22 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
23 eval line
, TOPLEVEL_BINDING
, "-e", lineno
27 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
31 opts
.on("-w", "--warn", "turn warnings on for your script") do
35 opts
.on("-I", "--include PATH",
36 "specify $LOAD_PATH (may be used more than once)") do |path
|
37 $LOAD_PATH.unshift(*path
.split(/:/))
40 opts
.on("-r", "--require LIBRARY",
41 "require the library, before executing your script") do |library
|
45 opts
.separator
"#{cmd} options:"
47 # some of these switches exist for rackup command-line compatibility,
49 opts
.on("-o", "--host HOST",
50 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
55 opts
.on("-p", "--port PORT", "use PORT (default: #{port})") do |p
|
60 opts
.on("-E", "--env RAILS_ENV",
61 "use RAILS_ENV for defaults (default: development)") do |e
|
65 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
66 daemonize
= d
? true : false
69 # Unicorn-specific stuff
70 opts
.on("-l", "--listen {HOST:PORT|PATH}",
71 "listen on HOST:PORT or PATH",
72 "this may be specified multiple times",
73 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
77 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
78 options
[:config_file] = f
81 opts
.on("-P PATH", "DEPRECATED") do |v
|
82 warn
%q{Use of -P is ambiguous and discouraged}
83 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
84 ENV['RAILS_RELATIVE_URL_ROOT'] = v
87 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
88 "(default: /)") do |v|
89 ENV['RAILS_RELATIVE_URL_ROOT'] = v
92 # I'm avoiding Unicorn-specific config options on the command-line.
93 # IMNSHO, config options on the command-line are redundant given
94 # config files and make things unnecessarily complicated with multiple
95 # places to look for a config option.
97 opts.separator "Common options:"
99 opts.on_tail("-h", "--help", "Show this message") do
100 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
104 opts.on_tail("-v", "--version", "Show version") do
105 puts " v#{Unicorn::Const::UNICORN_VERSION}"
112 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
114 if config && config =~ /\.ru$/
115 # parse embedded command-line options in config.ru comments
116 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
117 opts.parse! $1.split(/\s+/)
121 require 'pp' if $DEBUG
123 # this won't run until after forking if preload_app is false
125 # Load Rails and the private version of Rack it bundles.
127 require 'config/boot'
128 rescue LoadError => err
129 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
131 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
132 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
133 defined?(::Rails::VERSION::STRING) or
134 abort "Rails::VERSION::STRING not defined by config/boot"
136 inner_app = case config
138 require 'config/environment'
140 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
141 old_rails = case ::Rails::VERSION::MAJOR
143 when 2 then Rails::VERSION::MINOR < 3 ? true : false
149 require 'unicorn/app/old_rails'
150 Unicorn::App::OldRails.new
152 ActionController::Dispatcher.new
155 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
156 raw.sub!(/^__END__\n.*/, '')
157 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
160 Object.const_get(File.basename(config, '.rb').capitalize)
164 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
165 if inner_app.class.to_s == "Unicorn::App::OldRails"
167 # patches + tests welcome, but I really cbf to deal with this
168 # since all apps I've ever dealt with just use "/" ...
169 $stderr.puts "relative URL roots may not work for older Rails"
171 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
172 $stderr.puts "Debugger not available" if $DEBUG
174 use Unicorn::App::OldRails::Static
178 use Rails::Rack::LogTailer unless daemonize
179 use Rails::Rack::Debugger if $DEBUG
181 use Rails::Rack::Static
188 listeners << "#{host}:#{port}" if set_listener
192 :unicorn_options => options,
194 :daemonize => daemonize,
198 # ensure Rails standard tmp paths exist
199 %w(cache pids sessions sockets).each do |dir|
200 FileUtils.mkdir_p("tmp/#{dir}")
204 options[:pid] = rails_pid
205 Unicorn::Launcher.daemonize!
207 Unicorn.run(app, options)