Rack 簡介

Juanito FatasThinking about what to do.
 -----
|Browser| <= User, Request goes in the form of HTTP protocol.
 -----
|Server | <= Web Server,Give you the Response back.
 -----
|Rack   | <= Your middle man between server and app, so called Middleware.
 -----
|Rails  | <= Rails application stack.
 -----

Response = [Status, Header, Body]

其中:

Status: Integer
Header: Hash
Body:   Array
# Typical Response
[ 200, {"Content-Type" => "text/plain"}, ["Hello world!"] ]

可以用一句話總結:

Rack 是回應 #call 方法的 Ruby 物件,接受一個 hash 參數,將 status, header, body 以 Array 的形式返回。

config.ru 是 Rack 的設定檔。rackup 會讀取 config.ru,並啟動伺服器。

Sinatra、Ruby on Rails、幾乎所有用 Ruby 寫成的 web framework,都採用 Rack 作為 Middleware。