如何指定机架处理程序

发布于 2024-12-07 10:27:33 字数 633 浏览 3 评论 0原文

Rackup 已通过 Rack 的默认处理程序成功运行任何 Rack 应用程序。例如:

class RackApp  
  def call(environment)    
  [
    '200', 
    {'Content-Type' => 'text/html'}, 
    ["Hello world"]
  ]
  end 
end
run RackApp.new

但是当最后一行更改为使用 Rack 的内置 CGI 处理程序时,rackup 会给出“NoMethodError at / undefined method `call' for nil:NilClass”:

Rack::Handler::CGI.run RackApp.new

对于 Rack 的其他内置处理程序也提出了相同的反对意见。例如,Rack::Handler::Thin、Rack::Handler::FastCGI,甚至 Rack::Handler::WEBrick(这是 Rack 在默认模式下选择的处理程序)。

这里正确的语法是什么?

Rackup is successfully running any Rack app via Rack's default handler. e.g.:

class RackApp  
  def call(environment)    
  [
    '200', 
    {'Content-Type' => 'text/html'}, 
    ["Hello world"]
  ]
  end 
end
run RackApp.new

But rackup is giving "NoMethodError at / undefined method `call' for nil:NilClass" when the last line is changed to instead use Rack's built-in CGI handler:

Rack::Handler::CGI.run RackApp.new

The same objection is raised for Rack's other built-in handlers. e.g. Rack::Handler::Thin, Rack::Handler::FastCGI, even Rack::Handler::WEBrick (which is the handler Rack selects above in default mode).

What's the correct syntax here?

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

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

发布评论

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

评论(2

我早已燃尽 2024-12-14 10:27:33

rackup 命令读取配置文件并启动服务器。 Rack::Handler::XXX.run 方法启动服务器,独立于 rackup 命令(CGI 略有不同,因为它实际上不是服务器)。

当您将线路更改

run RackApp.new

Rack::Handler::CGI.run RackApp.new

并运行 rackup 时,会发生如下情况。服务器启动并解析配置文件。当到达 Rack::Handler::CGI.run RackApp.new 行时,它将像任何其他 Ruby 代码一样执行。对于 CGI 处理程序,它会调用应用程序并将输出写入标准输出,就像作为 CGI 脚本运行一样(运行rackup 时查看您的终端)。之后,“rackup”服务器正常启动,但没有应用程序运行。当您尝试访问该页面时,您将收到 NoMethodError,因为该应用程序为零。

使用 Rack::Handler::Thin 类似,但在本例中,由于 Thin 实际上是一个 Web 服务器,因此它会启动并为 RackApp 提供服务,但监听 Thin 的默认端口 8080(不是机架默认端口 9292)。停止 Thin 后(例如使用 Ctrl-C),默认的rackup 服务器(Mongrel 或 Webrick)将开始侦听端口 9292,同样没有指定应用程序,因此您将收到 NoMethodError。

如果您将修改后的“config.ru”作为普通 Ruby 脚本而不是使用rackup 运行,您将看到相同的行为,但不会启动rackup 服务器。 (您需要首先需要rack,因此请使用ruby -rrack config.ru)。在 CGI 情况下,对应用程序的单个调用的输出将打印到控制台,在 Thin 情况下 Thin 将开始为您的应用程序提供服务。

为了指定与rackup一起使用的服务器,您可以使用-s选项,例如rackup -s Thin将使用Thin启动应用程序(这次在rackup上)默认端口 9292)。您还可以执行rackup -s cgi,但这不会真正以任何有用的方式工作 - 它只是将错误页面的 html 打印到控制台。

CGI

如果您尝试将应用程序作为 CGI 运行,有几个选项。您需要创建一个使用 CGI 处理程序调用您的应用程序的 CGI 脚本。这本身可以是一个直接调用 Rack::Handler::CGI.run 的 ruby​​ 脚本,事实上,您可以直接使用修改后的 config.ru (您可能想要首先重命名它并添加显式的 require 'rack' 行)。

或者,您可以使用 shell 脚本,然后调用rackup config.ru。在这种情况下,rackup 检测到它正在作为 CGI 运行并自动使用正确的处理程序

The rackup command reads the config file and starts a server. The Rack::Handler::XXX.run methods also start a server, independently of the rackup command (CGI is slightly different as it isn't actually a server as such).

What happens when you change the line

run RackApp.new

to

Rack::Handler::CGI.run RackApp.new

and run rackup is as follows. The server starts and parses the config file. When the Rack::Handler::CGI.run RackApp.new line is reached it is executed as any other Ruby code would be. In the case of the CGI handler this calls the app and writes the output to the standard output as it would if running as a CGI script (have a look at your terminal when you run rackup). Afterwards the 'rackup' server is started as normal, but without an app to run. When you try to access the page you'll get the NoMethodError, since the app is nil.

Using Rack::Handler::Thin is similar, but in this case, as Thin actually is a web server, it is started and will serve RackApp, but listens on Thin's default port of 8080 (not the rack default of 9292). After stopping Thin (e.g. with Ctrl-C) the default rackup server (Mongrel or Webrick) will start listening on port 9292, again with no app specified so you'll get the NoMethodError.

If you run your modified 'config.ru' as a plain Ruby script rather than using rackup you'll see the same behaviour, but without the rackup server being started. (You'll need to require rack first, so use ruby -rrack config.ru). In the CGI case the output of a single call to your app will be printed to the console, in the Thin case Thin will be started serving your app.

In order to specify the server to use with rackup, you can use the -s option, e.g. rackup -s thin will start the app using Thin (this time on the rackup default port of 9292). You can also do rackup -s cgi but this won't really work in any useful way - it just prints out the html of an error page to the console.

CGI

If you're trying to run your app as a CGI there are a couple of options. You need to create a CGI script that calls your app using the CGI handler. This could itself be a ruby script that calls Rack::Handler::CGI.run directly, in fact you could use your modified config.ru directly (you might want to rename it first and add an explicit require 'rack' line).

Alternatively you can use a shell script which then calls rackup config.ru. In this situation rackup detects that it's running as CGI and automatically uses the correct handler

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