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

发布于 2024-10-15 13:30:55 字数 234 浏览 4 评论 0原文

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

I run the Thin webserver for basically every app in my dev/test environments. When I used Mongrel with Rails 2.x, all I had to type was script/server to get it to run the webserver I choose. But with Rails 3, I have to specify Thin every time. Is there a way to get Thin running on my Rails apps by just typing rails s instead of rails s thin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉殇 2024-10-22 13:30:55

是的,可以做到这一点。

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

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.

触ぅ动初心 2024-10-22 13:30:55

从 Rails 3.2rc2 开始,当 Gemfile 中存在 gem 'thin' 时,thin 现在默认在调用 rails server 时运行!感谢此拉取请求: https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

对我来说效果很好。

As of Rails 3.2rc2, thin is now run by default on invoking rails server when gem 'thin' is in your Gemfile! Thanks to this pull request: https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

Works great for me.

只为守护你 2024-10-22 13:30:55

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

In script/rails the following works as well:

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'
第几種人 2024-10-22 13:30:55

只需安装 Thin,cd 到您的应用程序所在的目录并运行 Thin Start。在这里完美运作。 :)

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

Simply install thin, cd to the directory that your app is in and run thin start. Works perfectly here. :)

You can use http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel to change as needed. (Its the one I used)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文