README: alter reply conventions for the mailing list
[rainbows.git] / bin / rainbows
blobb3f3a2673e7628dcee30dd7710392a1ac3a04223
1 #!/home/ew/bin/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'rainbows'
5 require 'optparse'
7 env = "development"
8 daemonize = false
9 listeners = []
10 options = { :listeners => listeners }
11 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
12 set_listener = false
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:"
20 lineno = 1
21 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
22 eval line, TOPLEVEL_BINDING, "-e", lineno
23 lineno += 1
24 end
26 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
27 $DEBUG = true
28 end
30 opts.on("-w", "--warn", "turn warnings on for your script") do
31 $-w = true
32 end
34 opts.on("-I", "--include PATH",
35 "specify $LOAD_PATH (may be used more than once)") do |path|
36 $LOAD_PATH.unshift(*path.split(/:/))
37 end
39 opts.on("-r", "--require LIBRARY",
40 "require the library, before executing your script") do |library|
41 require library
42 end
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|
50 host = h
51 set_listener = true
52 end
54 opts.on("-p", "--port PORT",
55 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
56 port = p.to_i
57 set_listener = true
58 end
60 opts.on("-E", "--env ENVIRONMENT",
61 "use ENVIRONMENT for defaults (default: development)") do |e|
62 env = e
63 end
65 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
66 daemonize = d ? true : false
67 end
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)
73 end
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"
78 end
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|
85 listeners << address
86 end
88 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
89 options[:config_file] = File.expand_path(f)
90 end
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, '')
101 exit
104 opts.on_tail("-v", "--version", "Show version") do
105 puts "unicorn v#{Unicorn::Const::UNICORN_VERSION}"
106 exit
109 opts.parse! ARGV
112 config = ARGV[0] || "config.ru"
113 abort "configuration file #{config} not found" unless File.exist?(config)
115 if config =~ /\.ru$/
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
124 app = lambda do ||
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
128 when /\.ru$/
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)
132 else
133 require config
134 Object.const_get(File.basename(config, '.rb').capitalize)
136 pp({ :inner_app => inner_app }) if $DEBUG
137 case env
138 when "development"
139 Rack::Builder.new do
140 use Rack::CommonLogger, $stderr
141 use Rack::ShowExceptions
142 use Rack::Lint
143 run inner_app
144 end.to_app
145 when "deployment"
146 Rack::Builder.new do
147 use Rack::CommonLogger, $stderr
148 run inner_app
149 end.to_app
150 else
151 inner_app
155 listeners << "#{host}:#{port}" if set_listener
157 if $DEBUG
158 pp({
159 :unicorn_options => options,
160 :app => app,
161 :daemonize => daemonize,
165 Unicorn::Launcher.daemonize! if daemonize
166 Rainbows.run(app, options)