Not only is Sinatra able to use other Rack middleware, any Sinatra application can in turn be added in front of any Rack endpoint as middleware itself.
This endpoint could be another Sinatra application, or any other Rack-based application (Rails/Ramaze/Camping/…):
before
filters are triggered
after
filters are executed after we've got a response back from the
route or the wrapped app
[~/sinatra/sinatra-as-middleware]$ cat app.rb require 'sinatra/base' require 'haml' require 'pp' class LoginScreen < Sinatra::Base enable :sessions enable :inline_templates get('/login') { haml :login } post('/login') do if params[:name] == 'admin' && params[:password] == 'admin' puts "params = " pp params session['user_name'] = params[:name] redirect '/' else redirect '/login' end end end class MyApp < Sinatra::Base enable :inline_templates # middleware will run before filters use LoginScreen before do unless session['user_name'] halt haml :denied end end get('/') do haml :cheer, :locals => { :name => session['user_name'] } end run! end __END__ @@ layout !!! %html %head %title Sinatra as Middleware %body %h1 Sinatra as Middleware = yield @@ login %form{:action=>'/login', :method=>'post'} %label{:for=>'name'} Name %input#name{:type=>"text", :name=>"name", :autofocus => true } %br %label{:for=>'password'} Password %input#password{:type=>"password", :name=>"password"} %br %button#go{:type=>"submit", :name=>"submit", :value=>"submit"} Click me! @@ cheer %h1 Hello #{name} %br @@ denied %h1 Access denied, please %a{:href=>'/login'}login.Casiano Rodriguez León 2015-01-07