【问题标题】:Is there a way Rails 3.0.x can default to using Thin?Rails 3.0.x 有没有办法默认使用 Thin?
【发布时间】:2011-01-31 16:33:26
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails ruby thin


    【解决方案1】:

    是的,可以这样做。

    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 时,我们会得到我们所追求的行为。

    【讨论】:

      【解决方案2】:

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

      非常适合我。

      【讨论】:

        【解决方案3】:

        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'
        

        【讨论】:

          【解决方案4】:

          只需将thin, cd 安装到您的应用所在的目录并运行thin start。在这里完美运行。 :)

          您可以根据需要使用http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel 进行更改。 (我用的是那个)

          【讨论】:

          • 作为更新,thin -V start 可以模拟您在启动 rails 服务器时通常看到的行为,也就是您在终端中看到每个连接的输出。
          • 这很酷。但是没有什么可以让rails s 运行thin start
          猜你喜欢
          • 2013-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-14
          相关资源
          最近更新 更多