Ruby rack
Rack 是 ruby 世界中提供 web 框架 (ruby on rails
, sinatra
) 和 web 伺服器 (webrick
, puma
, unicorn
) 之間介面的 gem。
The Rack interface
Respond to
call
method, which accepts a single argument calledenv
, and returns an array with three elements:status
,headers
,body
.
The rack hello world - class version
require 'rack'
class HelloWorld
def call(env)
[200, {'Content-Type' => 'text/plain'}, ['Hello World']]
end
end
Rack::Handler::WEBrick.run HelloWorld.new
# open http://localhost:8080
# Hello World
The rack hello world - lambda version
require 'rack'
app = -> (env) { [200, {'Content-Type' => 'text/plain'}, ['Hello World']] }
Rack::Handler::WEBrick.run app
# open http://localhost:8080
# Hello World
參考文獻