Ruby Rack 就是一个接口

hiveer · 2015年06月02日 · 最后由 swordray 回复于 2015年06月10日 · 3351 次阅读

Rack 通过句柄的机制来支持应用服务器

pry(main)> Rack::Handler.constants
=> [:CGI,
 :FastCGI,
 :Mongrel,
 :EventedMongrel,
 :SwiftipliedMongrel,
 :WEBrick,
 :LSWS,
 :SCGI,
 :Thin]

应用服务器也可以通过包含 Rack handler 来支持 Rack 接口 • Ebb • Fuzed • Glassfish v3 • Phusion Passenger (which is mod_rack for Apache and for nginx) • Rainbows! • Unicorn • Zbatery

Rack 是什么?

A Rack application is an Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: e status, the headers, and the body.

It takes exactly one argument, the environment and returns an Array of exactly three values: e status, the headers, and the body.

来个超级简单的例子:

rack_app = lambda{|env| [200,{},["hello from lambda"]]}
Rack::Handler::WEBrick.run rack_app ,:Port=>3000

这个时候到浏览器输入http://localhost:3000,那么将得到 hello from lambda

不知道你现在对 Rack 是什么认识?

这里有一篇类似的文章,创建一个自己的 Rack Server

@teddy 谢谢分享!

菜鸟弱弱的问一句哈,句柄到底是什么,怎么去理解,我看有文件句柄,数据库句柄....

需要 登录 后方可回复, 如果你还没有账号请 注册新账号