`

什么是Rack rails的中间件?

阅读更多

Introducing Rack

Tue Feb 20 18:26:03 CET 2007

Dabbling in my own web framework experiments, I noticed that there is a lot of code duplication among frameworks since they essentially all do the same things. And still, every Ruby web framework developer is writing his own handlers for every webserver he wants to use. Hopefully, the framework users are satisfied with that choice.

However, in essence, dealing with HTTP is rather easy. In the end, you get a request and return a response. Let’s do the easiest thing possible: The canonical format of a HTTP request probably is a hash of a CGI-like environment (that’s what most frameworks I’ve looked at deal with, internally), and a response consists of three parts: a status, a set of headers, and a body.

This could be easily mapped onto a method call in Ruby, looking like this:

class HelloWorld
  def call(env)
    [200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
  end
end

You’ve just seen the most simple Rack application.

Rack aims to provide a minimal API for connecting web servers and web frameworks.

Informally, a Rack application is a thing that responds to #call and takes a hash as argument, returning an array of status, headers and a body. The body needs to respond to #each and then successively return strings that represent the response body. The hash given contains a CGI-ish set of environment variables and some special values, like the body stream of the request (env['rack.input'] ), or information about the run-time environment (e.g. env['rack.run_once'] ).

Please note that this API is mainly used by framework developers and usually will not be exposed to framework users. It may seem a bit clumsy at first, or you may have expected a more “polished” API, but the important thing is that the API is very simple (notice that it even can be satisfied by a lambda ) and not hard to adopt. (The Camping adapter is a mere five lines of code.)

On top of this minimal API, there are libraries for commonly used things like query parsing or cookie handling and provide more convenience (Rack::Request and Rack::Response ) that you are free to use if you wish.

But the really cool thing about Rack is that it provides an extremely easy way to combine these web applications. After all, they are only Ruby objects with a single method that matters. And the thing that call s you must not really be a web server, but could as well be a different application! Let me show you a few Rack filters (or “middleware”) that already exist:

  • Rack::ShowExceptions catches all thrown exceptions and wraps them nicely in an helpful 500-page adapted from Django.

  • Rack::CommonLogger does Apache-style logs.

  • Rack::URLMap redirects to different Rack applications depending on the path and host (a very simple router).

There is another tool, Rack::Lint that checks if your applications and filters play nicely with others so everything ought to work together.

What do you gain if your web framework/server/application supports Rack?

  • Handlers for WEBrick, Mongrel and plain CGI (soon FastCGI, too), and every new webserver that provides a Rack handler. (Let n and m be the amount of servers and frameworks, without Rack it’s n*m, but with it’s n+m, which means less work for everyone.)

  • The possibility to run several applications inside a single webserver without external configuration.

  • Easier (integration and functional) testing, since everything can easily be mocked. (Helpers for this are coming soon, too.)

  • A greater diversity among frameworks, since writers now can concentrate on the parts that make it special and stop wasting their time on boring things.

  • More synergistic effects: Compare “That upload handler you wrote for IOWA is really great, too bad I use Camping.” with “That upload handler you wrote for Rack works great for me too!”

Currently, Rack is supported by Camping (adapter included with Rack) and Ramaze , more adapters are in the works (a Rails one would be a really cool contribution, hint, hint).

(If you are into Python, you’ll notice a lot of similarities between Rack and WSGI and Paste . That’s fully intended, as I think WSGI helped the Python web landscape really a lot.)

A Rubyforge project has been requested, and a first release will happen really soon. If you’d like to help, contact me by mail or via IRC (chris2@#ruby-lang on FreeNode).

分享到:
评论

相关推荐

    Ruby on Rails中Rack中间件的基础学习教程

    ### Ruby on Rails中Rack中间件的基础学习教程 #### 一、Rack简介 Rack是一种轻量级的Web服务器接口规范,它定义了Web服务器和Web应用之间的接口,使得不同的Web应用能够运行在同一服务器上。Rack的重要性在于它...

    Ruby-OmniAuth利用Rack中间件的一个灵活认证系统

    OmniAuth 就是一个 Rack 中间件,它可以插在 Web 应用程序的请求处理流程中,负责处理与用户认证相关的任务。 OmniAuth 的核心设计原则是模块化和可扩展性。它允许开发者定义多个“策略”(strategies),每个策略...

    用于检查应用程序健康状况的简单Rack中间件 -Ruby-Shell-下载

    标题中的“用于检查应用程序健康状况的简单Rack中间件”是指一种基于Ruby的Web服务器接口——Rack。Rack是一个规范,它定义了Ruby web应用和web服务器之间的接口,允许不同的框架和服务器之间进行交互。这个中间件是...

    作为Rack中间件的健康检查页面-site-inspector-demo-word-to-markdown-server-下载

    在IT行业中,Rack是一个非常重要的组件,尤其是在Ruby on Rails框架中。Rack是一个轻量级的接口,它定义了Web服务器如何与Ruby应用程序进行通信。这个“作为Rack中间件的健康检查页面”指的是使用Rack中间件来创建一...

    rack-zippy:用于提供gzip文件的机架中间件

    rack-zippy v4 +是Rack中间件,用于在不是Rails 4.2+应用程序的Rack应用程序中提供.gz文件。 (如果需要在Rails <= 4.1应用程序中使用rack-zippy,然后使用v3.0的rack-zippy,请在此处查看自述文件: : ) rack-...

    akita-rails-har-logger:秋田的Rails中间件

    这提供了Rack中间件和Rails ActionController过滤器,用于将HTTP请求-响应对记录到HAR文件中。 安装 将此行添加到您的应用程序的Gemfile中: gem 'akita-har_logger' 然后执行: $ bundle install 或将其自己安装...

    Ruby-Garner一组Rack中间件和缓存帮手实现不同的缓存策略

    Ruby-Garner是一个针对Ruby开发者的工具包,它包含了Rack中间件和一系列辅助方法,旨在帮助开发者实施高效的缓存策略。在Web开发中,缓存是提高性能的关键技术,它可以减少服务器处理请求的负担,加快用户访问速度,...

    rack-authorize:用于授权api访问的Rack中间件

    Rack :: Authorize是用于授权api访问的Rack中间件。 我们知道有Cancan,它是Ruby on Rails的出色授权库。 但是Cancan的规则是为Ruby Class定义的,当我们创建Web服务时,限制对api端点的访问是一项常见的任务。 这...

    OmniAuth是一个使用Rack中间件的灵活的身份验证系统。.zip

    OmniAuth是一个强大的身份验证解决方案,它利用了Rack中间件的灵活性,为Ruby on Rails和其他使用Rack的应用程序提供了一种标准化的方式处理多种身份验证服务。Rack是Ruby web服务器接口,允许开发者构建自己的web...

    rails_autoscale_agent:Rails Autoscale Heroku附加组件的Rails中间件gem

    这会将代理插入到您的Rack中间件堆栈中。 仅当存在RAILS_AUTOSCALE_URL ENV变量时,代理才会与Rails Autoscale通信,该变量在您安装Heroku附加组件时自动发生。 如果RAILS_AUTOSCALE_URL不存在,例如在开发中或...

    rack教程(ruby)

    什么是Rack? Rack是Ruby世界中的一个轻量级、灵活的Web服务器接口规范,它定义了一个统一的标准,使得Ruby Web应用能够与不同的Web服务器无缝集成。Rack简化了Ruby Web应用的部署过程,同时也提供了强大的功能和...

    weixin_rails_middleware, 微信集成 ruby weixin_rails_middleware for integration weixin..zip

    `weixin_rails_middleware` 是基于 Ruby 的 Rack 技术构建的,它能够插入到 Rails 应用的请求处理流程中。当收到微信服务器发来的请求时,中间件会自动处理这些请求,如验证签名、解析XML数据,并提供相应的响应。 ...

    Rails进行敏捷Web开发(所有版本的源码rails3.0-4.0)

    同时,它引入了多路由引擎支持,如Rack中间件,使得与其他Web服务器的集成更加容易。此外,ActiveRecord的查询接口(ARel)和ActiveModel的引入增强了模型层的功能和可扩展性。 2. Rails 3.1: 这个版本引入了Asset ...

    obscenity:Obscenity是RubyRubinius,Rails(通过ActiveModel)和Rack中间件的亵渎性过滤器

    Obscenity是Ruby / Rubinius,Rails(通过ActiveModel)和Rack中间件的亵渎性过滤器。 安装 将此行添加到您的应用程序的Gemfile中: gem 'obscenity' 然后执行: bundle install 或将其自己安装为: gem ...

    rack-perftools_profiler, 使用 perftools.rb 配置 rack 兼容应用程序的中间件.zip

    rack-perftools_profiler, 使用 perftools.rb 配置 rack 兼容应用程序的中间件 Rack::PerftoolsProfiler使用 perftools.rb 配置机架兼容应用的中间件快速启动假定应用程序使用 Rails 3 ( 你已经在下一节中安装了需求...

    rails api(文档)

    9. **Rate Limiting**:防止恶意用户或脚本滥用API,Rails API可以通过中间件(如Rack::Attack)实现请求速率限制。 10. **测试**:测试是API开发的关键部分,Rails API支持RSpec、Minitest等多种测试框架,方便...

    rails-beginner-s-guide

    Rails中间件负责应用中各个层次的请求处理逻辑。书中介绍了如何查看和定制中间件,以及如何构建自定义的中间件,这部分内容对于理解Rails应用的运行机制以及请求处理流程至关重要。 Rails中的安全性也是本书的重要...

    rack-bearer_auth:在机架应用程序中使用RFC 6750承载身份验证的中间件

    Rack :: BearerAuth是在Rack应用中使用承载身份验证的中间件。 安装 将此行添加到您的应用程序的Gemfile中: gem 'rack-bearer_auth' 然后执行: $ bundle 或将其自己安装为: $ gem install rack-bearer_auth ...

    rack-cas:Rack-CAS是用于执行CAS客户端身份验证的简单Rack中间件

    机架式CAS Rack-CAS是执行客户端身份验证的简单中间件。特征基于机架独立于框架的框架适用于,但不依赖于Rails,Sinatra等。 最小的依赖项当前的gem依赖项是 ,和 。 支持CAS额外属性额外属性虽然一团糟。 因此,请...

Global site tag (gtag.js) - Google Analytics