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