favor more string literals for cold call sites
[unicorn.git] / bin / unicorn
blob3c5e5cb2a53e70f67b4ee3975b0f293fa47b67ca
1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
6 ENV["RACK_ENV"] ||= "development"
7 rackup_opts = Unicorn::Configurator::RACKUP
8 options = rackup_opts[:options]
10 op = OptionParser.new("", 24, ' ') do |opts|
11 cmd = File.basename($0)
12 opts.banner = "Usage: #{cmd} " \
13 "[ruby options] [#{cmd} options] [rackup config file]"
14 opts.separator "Ruby options:"
16 lineno = 1
17 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
18 eval line, TOPLEVEL_BINDING, "-e", lineno
19 lineno += 1
20 end
22 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
23 $DEBUG = true
24 end
26 opts.on("-w", "--warn", "turn warnings on for your script") do
27 $-w = true
28 end
30 opts.on("-I", "--include PATH",
31 "specify $LOAD_PATH (may be used more than once)") do |path|
32 $LOAD_PATH.unshift(*path.split(':'))
33 end
35 opts.on("-r", "--require LIBRARY",
36 "require the library, before executing your script") do |library|
37 require library
38 end
40 opts.separator "#{cmd} options:"
42 # some of these switches exist for rackup command-line compatibility,
44 opts.on("-o", "--host HOST",
45 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
46 rackup_opts[:host] = h
47 rackup_opts[:set_listener] = true
48 end
50 opts.on("-p", "--port PORT", Integer,
51 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |port|
52 rackup_opts[:port] = port
53 rackup_opts[:set_listener] = true
54 end
56 opts.on("-E", "--env RACK_ENV",
57 "use RACK_ENV for defaults (default: development)") do |e|
58 ENV["RACK_ENV"] = e
59 end
61 opts.on("-N", "--no-default-middleware",
62 "do not load middleware implied by RACK_ENV") do |e|
63 rackup_opts[:no_default_middleware] = true
64 end
66 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
67 rackup_opts[:daemonize] = !!d
68 end
70 opts.on("-P", "--pid FILE", "DEPRECATED") do |f|
71 warn %q{Use of --pid/-P is strongly discouraged}
72 warn %q{Use the 'pid' directive in the Unicorn config file instead}
73 options[:pid] = f
74 end
76 opts.on("-s", "--server SERVER",
77 "this flag only exists for compatibility") do |s|
78 warn "-s/--server only exists for compatibility with rackup"
79 end
81 # Unicorn-specific stuff
82 opts.on("-l", "--listen {HOST:PORT|PATH}",
83 "listen on HOST:PORT or PATH",
84 "this may be specified multiple times",
85 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
86 options[:listeners] << address
87 end
89 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
90 options[:config_file] = f
91 end
93 # I'm avoiding Unicorn-specific config options on the command-line.
94 # IMNSHO, config options on the command-line are redundant given
95 # config files and make things unnecessarily complicated with multiple
96 # places to look for a config option.
98 opts.separator "Common options:"
100 opts.on_tail("-h", "--help", "Show this message") do
101 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
102 exit
105 opts.on_tail("-v", "--version", "Show version") do
106 puts "#{cmd} v#{Unicorn::Const::UNICORN_VERSION}"
107 exit
110 opts.parse! ARGV
113 app = Unicorn.builder(ARGV[0] || 'config.ru', op)
114 op = nil
116 if $DEBUG
117 require 'pp'
118 pp({
119 :unicorn_options => options,
120 :app => app,
121 :daemonize => rackup_opts[:daemonize],
125 Unicorn::Launcher.daemonize!(options) if rackup_opts[:daemonize]
126 Unicorn::HttpServer.new(app, options).start.join