2008-11-25 14:26:38 -05:00
|
|
|
require 'action_controller'
|
|
|
|
|
2006-03-25 22:29:09 -05:00
|
|
|
require 'fileutils'
|
2008-11-22 15:48:32 -05:00
|
|
|
require 'optparse'
|
2005-11-07 05:25:36 -05:00
|
|
|
|
2008-11-22 15:48:32 -05:00
|
|
|
# TODO: Push Thin adapter upstream so we don't need worry about requiring it
|
2005-11-07 08:31:19 -05:00
|
|
|
begin
|
2008-11-22 15:48:32 -05:00
|
|
|
require_library_or_gem 'thin'
|
2005-11-07 08:31:19 -05:00
|
|
|
rescue Exception
|
2008-11-22 15:48:32 -05:00
|
|
|
# Thin not available
|
2005-11-07 08:31:19 -05:00
|
|
|
end
|
|
|
|
|
2008-11-22 15:48:32 -05:00
|
|
|
options = {
|
|
|
|
:Port => 3000,
|
|
|
|
:Host => "0.0.0.0",
|
|
|
|
:environment => (ENV['RAILS_ENV'] || "development").dup,
|
|
|
|
:config => RAILS_ROOT + "/config.ru",
|
|
|
|
:detach => false,
|
2009-02-05 16:19:40 -05:00
|
|
|
:debugger => false,
|
|
|
|
:path => nil
|
2008-11-22 15:48:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ARGV.clone.options do |opts|
|
|
|
|
opts.on("-p", "--port=port", Integer,
|
|
|
|
"Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v }
|
|
|
|
opts.on("-b", "--binding=ip", String,
|
|
|
|
"Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v }
|
|
|
|
opts.on("-c", "--config=file", String,
|
|
|
|
"Use custom rackup configuration file") { |v| options[:config] = v }
|
|
|
|
opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:detach] = true }
|
|
|
|
opts.on("-u", "--debugger", "Enable ruby-debugging for the server.") { options[:debugger] = true }
|
|
|
|
opts.on("-e", "--environment=name", String,
|
|
|
|
"Specifies the environment to run this server under (test/development/production).",
|
|
|
|
"Default: development") { |v| options[:environment] = v }
|
2009-02-05 16:19:40 -05:00
|
|
|
opts.on("-P", "--path=/path", String, "Runs Rails app mounted at a specific path.", "Default: /") { |v| options[:path] = v }
|
2008-11-22 15:48:32 -05:00
|
|
|
|
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
|
|
|
|
|
|
|
|
opts.parse!
|
2006-06-28 19:41:29 -04:00
|
|
|
end
|
|
|
|
|
2008-11-22 15:48:32 -05:00
|
|
|
server = Rack::Handler.get(ARGV.first) rescue nil
|
|
|
|
unless server
|
|
|
|
begin
|
|
|
|
server = Rack::Handler::Mongrel
|
|
|
|
rescue LoadError => e
|
|
|
|
server = Rack::Handler::WEBrick
|
|
|
|
end
|
2008-06-25 17:40:00 -04:00
|
|
|
end
|
|
|
|
|
2008-11-22 15:48:32 -05:00
|
|
|
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
|
2009-02-05 16:19:40 -05:00
|
|
|
puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}#{options[:path]}"
|
2008-11-22 15:48:32 -05:00
|
|
|
|
|
|
|
%w(cache pids sessions sockets).each do |dir_to_make|
|
|
|
|
FileUtils.mkdir_p(File.join(RAILS_ROOT, 'tmp', dir_to_make))
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:detach]
|
|
|
|
Process.daemon
|
|
|
|
pid = "#{RAILS_ROOT}/tmp/pids/server.pid"
|
|
|
|
File.open(pid, 'w'){ |f| f.write(Process.pid) }
|
|
|
|
at_exit { File.delete(pid) if File.exist?(pid) }
|
|
|
|
end
|
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = options[:environment]
|
|
|
|
RAILS_ENV.replace(options[:environment]) if defined?(RAILS_ENV)
|
|
|
|
|
|
|
|
if File.exist?(options[:config])
|
|
|
|
config = options[:config]
|
|
|
|
if config =~ /\.ru$/
|
|
|
|
cfgfile = File.read(config)
|
|
|
|
if cfgfile[/^#\\(.*)/]
|
|
|
|
opts.parse!($1.split(/\s+/))
|
2005-11-07 05:25:36 -05:00
|
|
|
end
|
2008-12-01 13:21:18 -05:00
|
|
|
inner_app = eval("Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, config)
|
2008-11-22 15:48:32 -05:00
|
|
|
else
|
|
|
|
require config
|
2008-12-01 13:21:18 -05:00
|
|
|
inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
|
2008-11-22 15:48:32 -05:00
|
|
|
end
|
|
|
|
else
|
2008-12-01 13:21:18 -05:00
|
|
|
require RAILS_ROOT + "/config/environment"
|
2008-12-01 13:24:02 -05:00
|
|
|
inner_app = ActionController::Dispatcher.new
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-02-05 16:19:40 -05:00
|
|
|
if options[:path].nil?
|
|
|
|
map_path = "/"
|
|
|
|
else
|
|
|
|
ActionController::Base.relative_url_root = options[:path]
|
|
|
|
map_path = options[:path]
|
|
|
|
end
|
|
|
|
|
2008-12-01 13:21:18 -05:00
|
|
|
app = Rack::Builder.new {
|
2008-12-18 19:53:53 -05:00
|
|
|
use Rails::Rack::LogTailer unless options[:detach]
|
2008-12-01 13:21:18 -05:00
|
|
|
use Rails::Rack::Debugger if options[:debugger]
|
2009-02-05 16:19:40 -05:00
|
|
|
map map_path do
|
|
|
|
use Rails::Rack::Static
|
|
|
|
run inner_app
|
|
|
|
end
|
2008-12-01 13:21:18 -05:00
|
|
|
}.to_app
|
|
|
|
|
2008-11-22 15:48:32 -05:00
|
|
|
puts "=> Call with -d to detach"
|
|
|
|
|
|
|
|
trap(:INT) { exit }
|
|
|
|
|
|
|
|
puts "=> Ctrl-C to shutdown server"
|
|
|
|
|
|
|
|
begin
|
|
|
|
server.run(app, options.merge(:AccessLog => []))
|
|
|
|
ensure
|
|
|
|
puts 'Exiting'
|
|
|
|
end
|