tee_input: do not clobber trailer buffer on partial uploads
[unicorn.git] / bin / unicorn
blob225e8191c733a814246c596e1f700373392a6428
1 #!/home/ew/bin/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
6 ENV["RACK_ENV"] ||= "development"
7 daemonize = false
8 listeners = []
9 options = { :listeners => listeners }
10 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
11 set_listener = false
13 opts = OptionParser.new("", 24, ' ') do |opts|
14 opts.banner = "Usage: #{File.basename($0)} " \
15 "[ruby options] [unicorn options] [rackup config file]"
17 opts.separator "Ruby options:"
19 lineno = 1
20 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
21 eval line, TOPLEVEL_BINDING, "-e", lineno
22 lineno += 1
23 end
25 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
26 $DEBUG = true
27 end
29 opts.on("-w", "--warn", "turn warnings on for your script") do
30 $-w = true
31 end
33 opts.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path|
35 $LOAD_PATH.unshift(*path.split(/:/))
36 end
38 opts.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library|
40 require library
41 end
43 opts.separator "Unicorn options:"
45 # some of these switches exist for rackup command-line compatibility,
47 opts.on("-o", "--host HOST",
48 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
49 host = h
50 set_listener = true
51 end
53 opts.on("-p", "--port PORT",
54 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
55 port = p.to_i
56 set_listener = true
57 end
59 opts.on("-E", "--env ENVIRONMENT",
60 "use ENVIRONMENT for defaults (default: development)") do |e|
61 ENV["RACK_ENV"] = e
62 end
64 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
65 daemonize = d ? true : false
66 end
68 opts.on("-P", "--pid FILE", "DEPRECATED") do |f|
69 warn %q{Use of --pid/-P is strongly discouraged}
70 warn %q{Use the 'pid' directive in the Unicorn config file instead}
71 options[:pid] = File.expand_path(f)
72 end
74 opts.on("-s", "--server SERVER",
75 "this flag only exists for compatibility") do |s|
76 warn "-s/--server only exists for compatibility with rackup"
77 end
79 # Unicorn-specific stuff
80 opts.on("-l", "--listen {HOST:PORT|PATH}",
81 "listen on HOST:PORT or PATH",
82 "this may be specified multiple times",
83 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
84 listeners << address
85 end
87 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
88 options[:config_file] = File.expand_path(f)
89 end
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, '')
100 exit
103 opts.on_tail("-v", "--version", "Show version") do
104 puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
105 exit
108 opts.parse! ARGV
111 config = ARGV[0] || "config.ru"
112 abort "configuration file #{config} not found" unless File.exist?(config)
114 if 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 app = lambda do ||
124 # require Rack as late as possible in case $LOAD_PATH is modified
125 # in config.ru or command-line
126 inner_app = case config
127 when /\.ru$/
128 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
129 raw.sub!(/^__END__\n.*/, '')
130 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
131 else
132 require config
133 Object.const_get(File.basename(config, '.rb').capitalize)
135 pp({ :inner_app => inner_app }) if $DEBUG
136 case ENV["RACK_ENV"]
137 when "development"
138 Rack::Builder.new do
139 use Rack::CommonLogger, $stderr
140 use Rack::ShowExceptions
141 use Rack::Lint
142 run inner_app
143 end.to_app
144 when "deployment"
145 Rack::Builder.new do
146 use Rack::CommonLogger, $stderr
147 run inner_app
148 end.to_app
149 else
150 inner_app
154 listeners << "#{host}:#{port}" if set_listener
156 if $DEBUG
157 pp({
158 :unicorn_options => options,
159 :app => app,
160 :daemonize => daemonize,
164 Unicorn::Launcher.daemonize! if daemonize
165 Unicorn.run(app, options)