使用机架

In the previous article, you learned what Rack is. Now, it’s time to start using Rack and serve up some pages.

上一篇文章中 ,您了解了Rack是什么。 现在,是时候开始使用Rack并提供一些页面了。

你好,世界 ( Hello World )

First, let’s start with a “Hello world” application. This application will, no matter what type of request it’s given, return ​with a status code of 200 (which is HTTP-speak for “OK”) and the string ”Hello world” as the body.

首先,让我们从“ Hello world ”应用程序开始。 无论发出何种类型的请求,此应用程序都将返回状态代码200(以HTTP表示``OK''),并以字符串“ Hello world”作为正文。

Before examining the following code, consider again the requirements that any Rack application must meet.

在检查以下代码之前,请再次考虑任何Rack应用程序必须满足的要求。

A Rack application is any Ruby object that responds to the call method, takes a single hash parameter and returns an array containing the response status code, HTTP response headers and the response body as an array of strings.
Rack应用程序是任何对call方法作出响应的Ruby对象,它采用单个哈希参数并返回一个包含响应状态代码,HTTP响应标头和响应主体的字符串数组。

def call(env)
def呼叫(env)
return [200, {}, ["Hello world!"]]
return [200,{},[“ Hello world!”]]
end
结束
end
结束

As you can see, an object of the type HelloWorld will meet all of these requirements. It does so in a very minimal and not terribly useful way, but it does meet all of the requirements.

如您所见, HelloWorld类型的对象将满足所有这些要求。 它以一种非常小巧且不是非常有用的方式来执行此操作,但是它确实满足了所有要求。

WEBrick ( WEBrick )

That’s pretty simple, now let’s plug it into WEBrick (the HTTP server that comes with Ruby). To do this, we use the Rack::Handler::WEBrick.run method, pass it an instance of HelloWorld and the port to run on. A WEBrick server will now be running, and Rack will be passing requests between the HTTP server and your application.

这非常简单,现在让我们将其插入WEBrick( Ruby随附的HTTP服务器)中。 为此,我们使用Rack :: Handler :: WEBrick.run方法,将其传递给HelloWorld实例以及要在其上运行的端口。 WEBrick服务器现在将运行,Rack将在HTTP服务器和您的应用程序之间传递请求。

Note, this isn’t an ideal way to launch things with Rack. It's only shown here to get something running before diving into another feature of Rack called "Rackup," which is shown below. Using Rack::Handler in this way has a few problems. First, it’s not very configurable. Everything is hard-coded into the script. Second, as you’ll notice if you run the following script, you can’t kill the program. It won’t respond to Ctrl-C. If you run this command, simply close the terminal window and open a new one.

请注意,这不是使用Rack启动事物的理想方法。 在进入Rack的另一个功能“ Rackup”之前,这里仅显示了要运行的功能,如下所示。 以这种方式使用Rack :: Handler有一些问题。 首先,它不是非常可配置的。 一切都被硬编码到脚本中。 其次,您会注意到,如果您运行以下脚本,则无法终止该程序。 它不会响应Ctrl-C。 如果运行此命令,只需关闭终端窗口并打开一个新窗口。


require 'rack'
需要“机架”
class HelloWorld
HelloWorld类
def call(env)
def呼叫(env)
return [200, {}, ["Hello world!"]]
return [200,{},[“ Hello world!”]]
end
结束
end
结束
Rack::Handler::WEBrick.run(
机架::处理程序:: WEBrick.run(
HelloWorld.new,
HelloWorld.new,
:Port => 9000
:端口=> 9000
)
)

架起来 ( Rackup )

While this is quite easy to do, it isn’t how Rack is normally used. Rack is normally used with a tool called rackup. Rackup does more or less what was in the bottom section of the code above, but in a more usable way. Rackup is run from the command-line, and is given a .ru “Rackup file.” This is just a Ruby script that, among other things, feeds an application to Rackup.

尽管这很容易做到,但通常不是Rack的用法。 机架通常与称为rackup的工具一起使用 。 Rackup或多或少地执行了上面代码底部的内容,但是以一种更有用的方式进行。 Rackup从命令行运行,并提供一个.ru “ Rackup文件”。 这只是一个Ruby脚本,除其他外,它还向Rackup提供了应用程序。

A very basic Rackup file for the above would look something like this.

上面的一个非常基本的Rackup文件看起来像这样。


def call(env)
def呼叫(env)
return [
返回[
200,
200,
{'Content-Type' => 'text/html'},
{'Content-Type'=>'text / html'},
["Hello world!"]
[“你好,世界!”]
]
]
end
结束
end
结束
run HelloWorld.new
运行HelloWorld.new

First, we had to make one tiny change to the HelloWorld class. Rackup is running a middleware app called Rack::Lint that sanity-checks responses. All HTTP responses should have a Content-Type header, so that was added. Then, the last line just creates an instance of the app and passes it to the run method. Ideally, your application shouldn’t be written entirely within the Rackup file, this file should require your application into it and create an instance of it that way. The Rackup file is just “glue,” no real application code should be there.

首先,我们必须对HelloWorld类进行微小的更改。 Rackup正在运行一个名为Rack :: Lint的中间件应用程序,该应用程序可以合理地检查响应。 所有HTTP响应都应具有Content-Type标头,因此已添加。 然后,最后一行仅创建该应用程序的一个实例,并将其传递给run方法。 理想情况下,您的应用程序不应完全写在Rackup文件中,该文件应要求您的应用程序进入该文件并以此方式创建其实例。 Rackup文件只是“胶水”,没有真实的应用程序代码。

If you run the command rackup helloworld.ru, it’ll start a server on port 9292. This is the default Rackup port.

如果运行命令rackup helloworld.ru ,它将在端口9292上启动服务器。这是默认的Rackup端口。

Rackup has some more useful features. First, things like the port can be changed on the command line, or in a special line in the script. On the command-line, simply pass in a -p port parameter. For example: rackup -p 1337 helloworld.ru. From the script itself, if the first line starts with #\, then it’s parsed just like the command line. So you can define options here as well. If you wanted to run on port 1337, the first line of the Rackup file could read #\ -p 1337.

Rackup具有一些更有用的功能。 首先,可以在命令行或脚本的特殊行中更改端口之类的内容。 在命令行上,只需传入-p端口参数即可。 例如: rackup -p 1337 helloworld.ru 。 从脚本本身来看,如果第一行以#\开头,那么它将像命令行一样进行解析。 因此,您也可以在此处定义选项。 如果要在端口1337上运行,Rackup文件的第一行可能显示为#\ -p 1337

翻译自: https://www.thoughtco.com/using-rack-2908121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值