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
参考文献