rails: RAILS_RELATIVE_URL_ROOT may be set in Unicorn config
[unicorn.git] / bin / unicorn_rails
blobab9f1c05effad85bbaedb415ccec37014d81f511
1 #!/home/ew/bin/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
5 require 'fileutils'
7 rails_pid = "#{Unicorn::HttpServer::START_CTX[:cwd]}/tmp/pids/unicorn.pid"
8 cmd = File.basename($0)
9 daemonize = false
10 listeners = []
11 options = { :listeners => listeners }
12 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
13 set_listener = false
14 ENV['RAILS_ENV'] ||= "development"
16 opts = OptionParser.new("", 24, ' ') do |opts|
17 opts.banner = "Usage: #{cmd} " \
18 "[ruby options] [#{cmd} options] [rackup config file]"
19 opts.separator "Ruby options:"
21 lineno = 1
22 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
23 eval line, TOPLEVEL_BINDING, "-e", lineno
24 lineno += 1
25 end
27 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
28 $DEBUG = true
29 end
31 opts.on("-w", "--warn", "turn warnings on for your script") do
32 $-w = true
33 end
35 opts.on("-I", "--include PATH",
36 "specify $LOAD_PATH (may be used more than once)") do |path|
37 $LOAD_PATH.unshift(*path.split(/:/))
38 end
40 opts.on("-r", "--require LIBRARY",
41 "require the library, before executing your script") do |library|
42 require library
43 end
45 opts.separator "#{cmd} options:"
47 # some of these switches exist for rackup command-line compatibility,
49 opts.on("-o", "--host HOST",
50 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
51 host = h
52 set_listener = true
53 end
55 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
56 port = p.to_i
57 set_listener = true
58 end
60 opts.on("-E", "--env RAILS_ENV",
61 "use RAILS_ENV for defaults (default: development)") do |e|
62 ENV['RAILS_ENV'] = e
63 end
65 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
66 daemonize = d ? true : false
67 end
69 # Unicorn-specific stuff
70 opts.on("-l", "--listen {HOST:PORT|PATH}",
71 "listen on HOST:PORT or PATH",
72 "this may be specified multiple times",
73 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
74 listeners << address
75 end
77 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
78 options[:config_file] = File.expand_path(f)
79 end
81 opts.on("-P", "--path PATH", "DEPRECATED") do |v|
82 warn %q{Use of --path/-P is strongly discouraged}
83 warn %q{Use the RAILS_RELATIVE_URL_ROOT environment variable}
84 warn %q{ or the 'map' directive in the rackup config instead}
85 ENV['RAILS_RELATIVE_URL_ROOT'] = v
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.to_s.gsub(/^.*DEPRECATED.*$/s, '')
97 exit
98 end
100 opts.on_tail("-v", "--version", "Show version") do
101 puts " v#{Unicorn::Const::UNICORN_VERSION}"
102 exit
105 opts.parse! ARGV
108 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
110 if config && config =~ /\.ru$/
111 # parse embedded command-line options in config.ru comments
112 if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/
113 opts.parse! $1.split(/\s+/)
117 require 'pp' if $DEBUG
119 # this won't run until after forking if preload_app is false
120 app = lambda do ||
121 # Load Rails and the private version of Rack it bundles.
122 begin
123 require 'config/boot'
124 rescue LoadError => err
125 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
127 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
128 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
129 defined?(::Rails::VERSION::STRING) or
130 abort "Rails::VERSION::STRING not defined by config/boot"
132 inner_app = case config
133 when nil
134 require 'config/environment'
136 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
137 old_rails = case ::Rails::VERSION::MAJOR
138 when 0, 1 then true
139 when 2 then Rails::VERSION::MINOR < 3 ? true : false
140 else
141 false
144 if old_rails
145 require 'unicorn/app/old_rails'
146 Unicorn::App::OldRails.new
147 else
148 ActionController::Dispatcher.new
150 when /\.ru$/
151 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
152 raw.sub!(/^__END__\n.*/, '')
153 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
154 else
155 require config
156 Object.const_get(File.basename(config, '.rb').capitalize)
159 Rack::Builder.new do
160 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
161 if inner_app.class.to_s == "Unicorn::App::OldRails"
162 if map_path != '/'
163 # patches + tests welcome, but I really cbf to deal with this
164 # since all apps I've ever dealt with just use "/" ...
165 $stderr.puts "relative URL roots may not work for older Rails"
167 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
168 $stderr.puts "Debugger not available" if $DEBUG
169 map(map_path) do
170 require 'unicorn/app/old_rails/static'
171 use Unicorn::App::OldRails::Static
172 run inner_app
174 else
175 use Rails::Rack::LogTailer unless daemonize
176 use Rails::Rack::Debugger if $DEBUG
177 map(map_path) do
178 use Rails::Rack::Static
179 run inner_app
182 end.to_app
185 listeners << "#{host}:#{port}" if set_listener
187 if $DEBUG
188 pp({
189 :unicorn_options => options,
190 :app => app,
191 :daemonize => daemonize,
195 # ensure Rails standard tmp paths exist
196 %w(cache pids sessions sockets).each do |dir|
197 FileUtils.mkdir_p("tmp/#{dir}")
200 if daemonize
201 options[:pid] = rails_pid
202 Unicorn::Launcher.daemonize!
204 Unicorn.run(app, options)