Rack::Builder implements a small DSL to
iteratively construct Rack applications.
Example:
require 'rack/lobster'
app = Rack::Builder.new do
use Rack::CommonLogger
use Rack::ShowExceptions
map "/lobster" do
use Rack::Lint
run Rack::Lobster.new
end
end
run app
Or
app = Rack::Builder.app do
use Rack::CommonLogger
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
end
run app
use adds middleware to the stack, run dispatches to an
application. You can use map to construct a Rack::URLMap in a convenient way.
Public Class Methods
app(default_app = nil, &block)click to toggle source
# File lib/rack/builder.rb, line 58defself.app(default_app = nil, &block)
self.new(default_app, &block).to_append
new(default_app = nil,&block)click to toggle source
class Heartbeat
def self.call(env)
[200, { "Content-Type" => "text/plain" }, ["OK"]]
end
end
run Heartbeat
# File lib/rack/builder.rb, line 103defrun(app)
@run = append
to_app()click to toggle source
# File lib/rack/builder.rb, line 144defto_appapp = @map?generate_map(@run, @map) :@runfail"missing run or map statement"unlessappapp = @use.reverse.inject(app) { |a,e|e[a] }
@warmup.call(app) if@warmupappend
use(middleware, *args, &block)click to toggle source
Specifies middleware to use in a stack.
class Middleware
def initialize(app)
@app = app
end
def call(env)
env["rack.some_header"] = "setting an example"
@app.call(env)
end
end
use Middleware
run lambda { |env| [200, { "Content-Type" => "text/plain" }, ["OK"]] }
All requests through to this application will first be processed by the
middleware class. The call method in this example sets an
additional environment key which then can be referenced in the application
if required.