我基本上为我的开发/测试环境中的每个应用程序运行瘦网络服务器。当我在 Rails 2.x 中使用 Mongrel 时,我所要做的就是script/server
让它运行我选择的网络服务器。但是对于 Rails 3,我每次都必须指定 Thin。有没有办法通过键入rails s
而不是让 Thin 在我的 Rails 应用程序上运行rails s thin
?
4 回答
是的,有可能做到这一点。
该rails s
命令在一天结束时的工作方式是通过 Rack 并让它选择服务器。默认情况下,Rack 处理程序将尝试使用mongrel
,如果找不到 mongrel,它将使用webrick
. 我们所要做的就是稍微修补处理程序。我们需要将补丁插入rails
脚本本身。这就是你要做的,破解打开你的script/rails
文件。默认情况下,它应该如下所示:
#!/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'
我们在该require 'rails/commands'
行之前插入我们的补丁。我们的新文件应如下所示:
#!/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'
请注意,它现在将尝试 Mongrel,如果出现错误,请尝试 Thin,然后才使用 Webrick。现在,当您键入时,rails s
我们会得到我们所追求的行为。
rails server
从 Rails 3.2rc2 开始,在 Gemfile 中调用时,thin 现在默认运行gem 'thin'
!感谢这个拉取请求:https ://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7
对我很有用。
在script/rails
以下作品中也是如此:
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::Thin
require 'rails/commands'
只需将thin, cd 安装到您的应用程序所在的目录并运行thin start。在这里完美运行。:)
您可以根据需要使用http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel进行更改。(这是我用的那个)