有没有办法Rails 3.0.x可以默认使用Thin?

tub*_*bbo 13 ruby ruby-on-rails thin

我为我的开发/测试环境中的每个应用程序运行Thin Webserver.当我使用Mongrel和Rails 2.x时,我所要输入的只是script/server让它运行我选择的网络服务器.但是使用Rails 3,我必须每次都指定Thin.有没有办法让我的Rails应用程序只需键入rails s而不是rails s thin

sko*_*rks 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'
Run Code Online (Sandbox Code Playgroud)

我们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'
Run Code Online (Sandbox Code Playgroud)

请注意,它现在将尝试使用Mongrel,如果出现错误,请尝试使用Thin,然后再使用Webrick.现在当你输入时,rails s我们得到了我们所追求的行为.


tri*_*web 10

从Rails 3.2rc2开始,在你的Gemfile中调用rails server时,默认情况下会运行gem 'thin'thin!感谢这个拉取请求:https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

对我来说很棒.