2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
10 options
= { :listeners => listeners
}
11 host
, port
= Unicorn
::Const::DEFAULT_HOST, Unicorn
::Const::DEFAULT_PORT
14 opts
= OptionParser
.new("", 24, ' ') do |opts
|
15 opts
.banner
= "Usage: #{File.basename($0)} " \
16 "[ruby options] [unicorn 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
"Unicorn 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",
55 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p
|
60 opts
.on("-E", "--env ENVIRONMENT",
61 "use ENVIRONMENT for defaults (default: development)") do |e
|
65 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
66 daemonize
= d
? true : false
69 opts
.on("-P", "--pid FILE", "DEPRECATED") do |f
|
70 warn
%q{Use of --pid/-P is strongly discouraged}
71 warn %q{Use the 'pid' directive in the Unicorn config file instead}
72 options[:pid] = File.expand_path(f)
75 opts.on("-s", "--server SERVER",
76 "this flag only exists for compatibility") do |s|
77 warn "-s/--server only exists for compatibility with rackup"
80 # Unicorn-specific stuff
81 opts.on("-l", "--listen {HOST:PORT|PATH}",
82 "listen on HOST:PORT or PATH",
83 "this may be specified multiple times",
84 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
88 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
89 options[:config_file] = File.expand_path(f)
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 "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
112 config = ARGV[0] || "config.ru"
113 abort "configuration file #{config} not found" unless File.exist?(config)
116 # parse embedded command-line options in config.ru comments
117 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
118 opts.parse! $1.split(/\s+/)
122 require 'pp' if $DEBUG
125 # require Rack as late as possible in case $LOAD_PATH is modified
126 # in config.ru or command-line
127 inner_app = case config
129 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
130 raw.sub!(/^__END__\n.*/, '')
131 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
134 Object.const_get(File.basename(config, '.rb').capitalize)
136 pp({ :inner_app => inner_app }) if $DEBUG
140 use Rack::CommonLogger, $stderr
141 use Rack::ShowExceptions
147 use Rack::CommonLogger, $stderr
155 listeners << "#{host}:#{port}" if set_listener
159 :unicorn_options => options,
161 :daemonize => daemonize,
165 Unicorn::Launcher.daemonize! if daemonize
166 Rainbows.run(app, options)