examples/echo: "Expect:" value is case-insensitive
[unicorn.git] / bin / unicorn
blob8847e81d2e337019f2e9d97b562025d1d6b5daec
1 #!/home/ew/bin/ruby
2 require 'unicorn/launcher'
3 require 'optparse'
5 env = "development"
6 daemonize = false
7 listeners = []
8 options = { :listeners => listeners }
9 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
10 set_listener = false
12 opts = OptionParser.new("", 24, ' ') do |opts|
13 opts.banner = "Usage: #{File.basename($0)} " \
14 "[ruby options] [unicorn 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 "Unicorn 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 host = h
49 set_listener = true
50 end
52 opts.on("-p", "--port PORT",
53 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
54 port = p.to_i
55 set_listener = true
56 end
58 opts.on("-E", "--env ENVIRONMENT",
59 "use ENVIRONMENT for defaults (default: development)") do |e|
60 env = e
61 end
63 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
64 daemonize = d ? true : false
65 end
67 opts.on("-P", "--pid FILE", "file to store PID (default: none)") do |f|
68 options[:pid] = File.expand_path(f)
69 end
71 opts.on("-s", "--server SERVER",
72 "this flag only exists for compatibility") do |s|
73 warn "-s/--server only exists for compatibility with rackup"
74 end
76 # Unicorn-specific stuff
77 opts.on("-l", "--listen {HOST:PORT|PATH}",
78 "listen on HOST:PORT or PATH",
79 "this may be specified multiple times",
80 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
81 listeners << address
82 end
84 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
85 options[:config_file] = File.expand_path(f)
86 end
88 # I'm avoiding Unicorn-specific config options on the command-line.
89 # IMNSHO, config options on the command-line are redundant given
90 # config files and make things unnecessarily complicated with multiple
91 # places to look for a config option.
93 opts.separator "Common options:"
95 opts.on_tail("-h", "--help", "Show this message") do
96 puts opts
97 exit
98 end
100 opts.on_tail("-v", "--version", "Show version") do
101 puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
102 exit
105 opts.parse! ARGV
108 config = ARGV[0] || "config.ru"
109 abort "configuration file #{config} not found" unless File.exist?(config)
111 if config =~ /\.ru$/
112 # parse embedded command-line options in config.ru comments
113 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
114 opts.parse! $1.split(/\s+/)
118 require 'pp' if $DEBUG
120 app = lambda do ||
121 # require Rack as late as possible in case $LOAD_PATH is modified
122 # in config.ru or command-line
123 inner_app = case config
124 when /\.ru$/
125 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
126 raw.sub!(/^__END__\n.*/, '')
127 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
128 else
129 require config
130 Object.const_get(File.basename(config, '.rb').capitalize)
132 pp({ :inner_app => inner_app }) if $DEBUG
133 case env
134 when "development"
135 Rack::Builder.new do
136 use Rack::CommonLogger, $stderr
137 use Rack::ShowExceptions
138 use Rack::Lint
139 run inner_app
140 end.to_app
141 when "deployment"
142 Rack::Builder.new do
143 use Rack::CommonLogger, $stderr
144 run inner_app
145 end.to_app
146 else
147 inner_app
151 listeners << "#{host}:#{port}" if set_listener
153 if $DEBUG
154 pp({
155 :unicorn_options => options,
156 :app => app,
157 :daemonize => daemonize,
161 Unicorn::Launcher.daemonize! if daemonize
162 Unicorn.run(app, options)