add doc on using Rack middleware to README

This commit is contained in:
Ryan Tomayko 2008-05-19 17:25:09 -04:00
parent 07f2547331
commit bda21f1fbc
1 changed files with 24 additions and 0 deletions

View File

@ -349,6 +349,30 @@ When using send_file or static files you may have mime types Sinatra doesn't und
mime :foo, 'text/foo'
= Using Rack Middleware
Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard interface for Ruby web frameworks. One of Rack's most interesting capabilities for application developers is support for "middleware" -- components that sit between the server and your application monitoring and/or manipulating the HTTP request/response to provide various types of common functionality. What's more, middleware is portable between web frameworks, so middleware components developed under, e.g., Merb, can be used with Sinatra and vice versa.
Sinatra makes building Rack middleware pipelines a cinch via a top-level +use+ method:
require 'sinatra'
require 'my_custom_middleware'
use Rack::Lint
use MyCustomMiddleware
get '/hello' do
'Hello World'
end
The semantics of +use+ are identical to those defined for the Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL (most frequently used from rackup files). For example, the +use+ method accepts multiple/variable args as well as blocks:
use Rack::Auth::Basic do |username, password|
username == 'admin' && password == 'secret'
end
Rack is distributed with a variety of standard middleware for logging, debugging, URL routing, authentication, and session handling. Sinatra uses many of of these components automatically based on configuration so you typically don't have to +use+ them explicitly.
= Testing
=== Methods