博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rack 源码分析
阅读量:6328 次
发布时间:2019-06-22

本文共 3953 字,大约阅读时间需要 13 分钟。

hot3.png

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

它们的调用顺序是相反

refrence:

转载于:https://my.oschina.net/xieyunzi/blog/307685

你可能感兴趣的文章
4-3 简单求和 (10分)
查看>>
Python环境部署
查看>>
[BZOJ1927]星际竞速(费用流)
查看>>
为运维人员插上腾飞更远的翅膀!
查看>>
备用数据库快照
查看>>
编写shell脚本计划备份mysql数据库
查看>>
单页面应用简介
查看>>
Word 2003中编辑标记与格式标记大讨论
查看>>
从国内向海外转移域名经验谈
查看>>
浅谈apache与tomact的整合
查看>>
详解XStream别名
查看>>
SQL Server vNext CTP1 on Linux
查看>>
1-为 Lync Server 2010 准备 Active Directory 域服务
查看>>
Grid Control 10G安装与配置
查看>>
SELinux安全
查看>>
NetBackup下ORACLE恢复测试方案实例解析
查看>>
【有奖征文】“失业”程序员的苦辣酸甜
查看>>
IE9是如何被FireFox4超越全球市场份额的?
查看>>
linux bunzip2命令
查看>>
敏捷个人:通过实践TOGAF来思考如何学习并应用新的方法?
查看>>