2009年5月3日 星期日

Rails on Rack

Rails on Rack
Ruby on Rack #1 - Hello Rack!
Ruby on Rack #2 - The Builder 13
Introducing Rack
Rails meets Sinatra

What is Rack ?
Rack: a Ruby Webserver Interface
rack-contrib in github

Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.



Rack Specification

Rack specification specifies how exactly a Rack application and the web server should communicate :

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.



Rack Gem
Rack gem is a collection of utilities and facilitating classes, to make life easier for anyone developing Rack applications. It includes basic implementations of request, response, cookies & sessions. And a good number of usefult middlewares. In short, install the rack gem. You’re gonna need it :

$ sudo gem install rack



To summarize
Ruby on Rack #1 - Hello Rack!
* Rack is a framework to roll your own ruby framework.
* Rack provides an interface between different web servers and your framework/application. Making it very simple for your framework/application to be compatible with any webserver that supports Rack – Phusion Passenger, Litespeed, Mongrel, Thin, Ebb, Webrick to name a few.
* Rack cuts your chase. You get request, response, cookies, params & sessions for free.
* Makes it possible to use multiple frameworks for the same application, provided there is no class collision. Rails and sinatra integration is a good example of this.
* Middlewares ! Think of middlewares as Rails’s before_filter/after_filter that are reusable across different rack supported frameworks/applications. For example, you can use the same Anti-spamming rack middleware for your Rails app, Sinatra app and your custom Rack application too!



2.1 Rails Application’s Rack Object

ActionController::Dispatcher.new is the primary Rack application object of a Rails application. Any Rack compliant web server should be using ActionController::Dispatcher.new object to serve a Rails application.



2.2 script/server

script/server does the basic job of creating a Rack::Builder object and starting the webserver. This is Rails’ equivalent of Rack’s rackup script.

Here’s how script/server creates an instance of Rack::Builder



2.3 rackup

To use rackup instead of Rails’ script/server, you can put the following inside config.ru of your Rails application’s root directory:



3 Action Controller Middleware Stack
Rack Middleware railscast

Many of Action Controller’s internal components are implemented as Rack middlewares. ActionController::Dispatcher uses ActionController::MiddlewareStack to combine various internal and external middlewares to form a complete Rails Rack application.

ActionController::MiddlewareStack is Rails’ equivalent of Rack::Builder, but built for better flexibility and more features to meet Rails’ requirements.



3.2 Configuring Middleware Stack

Rails provides a simple configuration interface config.middleware for adding, removing and modifying the middlewares in the middleware stack via environment.rb or the environment specific configuration file environments/.rb.

3.2.1 Adding a Middleware


3.2.2 Swapping a Middleware

You can swap an existing middleware in the middleware stack using config.middleware.swap.




3.4 Customizing Internal Middleware Stack

It’s possible to replace the entire middleware stack with a custom stack using ActionController::Dispatcher.middleware=.



3.5 Using Rack Builder

The following shows how to replace use Rack::Builder instead of the Rails supplied MiddlewareStack.

Clear the existing Rails middleware stack
Add a config.ru file to RAILS_ROOT



4 Rails Metal Applications
Rails Metal railscast
Rails Metal: a micro-framework with the power of Rails
Introducing Rails Metal

Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.

沒有留言: