Rails 3.0.x是否有默认使用Thin的方法?

13

我在我的开发/测试环境中基本上为每个应用程序运行Thin Web服务器。当我在Rails 2.x中使用Mongrel时,我只需键入script/server即可运行我选择的Web服务器。但是在Rails 3中,我必须每次指定Thin。是否有一种方法可以通过键入rails s而不是rails s thin来运行我的Rails应用程序上的Thin?

4个回答

21

是的,可以这样做。

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时,我们得到了我们想要的行为。


10

1
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'

0

1
作为更新,thin -V start可以模仿您通常在启动Rails服务器时看到的行为,即从每个连接中在终端中看到输出。 - ddd
2
很酷。但是没有任何东西可以使rails s运行thin start吗? - tubbo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接