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: The status
, the headers
, and the body
.
status
is the HTTP status code.headers
is a hash of HTTP headers for the response.body
is the actual body of the response (e.g. the HTML you want to display). The body must also respond to each.
安装 rack
$ gem install rack
irb 中运行
2.0.0-p353 :001 > require 'rack'2.0.0-p353 :002 > Rack::Handler::WEBrick.run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['success']] }
简单例子
$ mkdir hellorack && cd hellorack
Now create a file named config.ru
and fill it in with the following:
class Hello def self.call(env) [ 200, # 200 indicates success {"Content-Type" => "text/plain"}, # the hash of headers ["Hello from Rack!"] # we wrap the body in an Array so # that it responds to `each` ] endend # Tell Rack what to run our apprun Hello
Save the file, open up your terminal and run the rackup command:
$ rackup[2012-12-21 17:48:38] INFO WEBrick 1.3.1[2012-12-21 17:48:38] INFO ruby 1.9.2 (2011-07-09) [x86_64-darwin11.0.1][2012-12-21 17:48:38] INFO WEBrick::HTTPServer#start: pid=1597 port=9292
更多使用实例
rackup 执行流程
rack/bin/rackup.rb
require "rack"Rack::Server.start
Rack::Server.start
可以通过传递参数,或是读取同级目录下的 config.ru
初始化 rack app,然后调用 rack server 的 run
方法启动 web server ,如下:
rack/lib/server.rb
module Rack class Server def self.start(options = nil) end def start(&blk) ... # 此处调用 Rack::Handler 实例 server.run wrapped_app, options, &blk end endend
rack
与 web server 的沟通中介 Rack::Handler
以 Rack::Handler::WEBrick
为例
module Rack module Handler class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet def self.run(app, options={}) ... @server = ::WEBrick::HTTPServer.new(options) @server.mount "/", Rack::Handler::WEBrick, app yield @server if block_given? @server.start end def initialize(server, app) super server @app = app end # 实现 service def service(req, res) ... status, headers, body = @app.call(env) ... end end endend
web server
以 webrick
为例
require 'webrick'options = { BindAddress: 'localhost', Port: 8080}server = WEBrick::HTTPServer.new(options)begin server.startensure server.shutdownend
调用流程
WEBrick::HTTPServer::new-> WEBrick::GenericServer#start(&block)-> WEBrick::HTTPServer#start_thread(sock, &block)-> WEBrick::HTTPServer#run(sock)
module WEBrick class HTTPServer < ::WEBrick::GenericServer def run(sock) while true ... server = lookup_server(req) || self ... server.service(req, res) ... res.send_response(sock) ... end end def service(req, res) ... servlet, options, script_name, path_info = search_servlet(req.path) ... si = servlet.get_instance(self, *options) ... si.service(req, res) end endend
创建 Rack app, Rack::Builder
app = Rack::Builder.app do use Rack::CommonLogger run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }endrun app
运行 Rack app, Rack::Server#start
Rack::Server.start app: app , Port: 3000
使用 DSL 简化创建 Rack app 的代码, Rack::Builder
通常的 Rack app 都是由多个 Rack middleware 嵌套而成的,使用 pure ruby 的写法就是:
app = MyRackApp.newapp = Rack::Lint.new(app)app = Rack::ShowStatus.new(app)app = Rack::ShowExceptions.new(app)app = Rack::CommonLogger.new(app)Rack::Handler::Thin.run app, :Port => 8080
Rack 提供了创建 Rack app 的 DSL
app = Rack::Builder.new do use Rack::CommonLogger use Rack::ShowExceptions use Rack::ShowStatus use Rack::Lint run MyRackApp.newendRack::Handler::Thin.run app, :Port => 8080
它们的调用顺序是相反的