Yeah it's possible to do this.
The way the rails s
command works at the end of the day is by falling through to Rack and letting it pick the server. By default the Rack handler will try to use mongrel
and if it can't find mongrel it will go with webrick
. All we have to do is patch the handler slightly. We'll need to insert our patch into the rails
script itself. Here is what you do, crack open your script/rails
file. By default it should look like this:
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
We insert our patch right before the require 'rails/commands'
line. Our new file should look like this:
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rack/handler'
Rack::Handler.class_eval do
def self.default(options = {})
# Guess.
if ENV.include?("PHP_FCGI_CHILDREN")
# We already speak FastCGI
options.delete :File
options.delete :Port
Rack::Handler::FastCGI
elsif ENV.include?("REQUEST_METHOD")
Rack::Handler::CGI
else
begin
Rack::Handler::Mongrel
rescue LoadError
begin
Rack::Handler::Thin
rescue LoadError
Rack::Handler::WEBrick
end
end
end
end
end
require 'rails/commands'
Notice that it will now try Mongrel and if there is an error try for Thin and only then go with Webrick. Now when you type rails s
we get the behaviour we're after.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…