Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2014 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/98e2f13dc6640ab5c486 to your computer and use it in GitHub Desktop.
Save anonymous/98e2f13dc6640ab5c486 to your computer and use it in GitHub Desktop.
require 'rack'
require 'pry'
class MyApp
def self.call(env)
request = Rack::Request.new(env)
if request.path == '/'
[200, {"Content-Type" => "text/html"}, ["<h1>You're at the root path!!!!</h1>"]]
else
[404, {"Content-Type" => "text/html"}, ["File Not Found"]]
end
end
end
class MySecurityApp
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.params['password'] == 'secret'
@app.call(env)
else
[401, {"Content-Type" => "text/html"}, ["Forbidden"]]
end
end
end
use MySecurityApp
run MyApp
require 'rack'
class TopApp
def initialize(app)
puts "Initialing TopApp"
puts "Passed app is currently #{app}"
@app = app
end
def call(env)
puts "Called TopApp"
@app.call(env)
end
end
class MiddleApp
def initialize(app)
puts "Initialing MiddleApp"
puts "Passed app is currently #{app}"
@app = app
end
def call(env)
puts "Called MiddleApp"
@app.call(env)
end
end
class BottomApp
def initialize(app)
puts "Initialing BottomApp"
puts "Passed app is currently #{app}"
@app = app
end
def call(env)
puts "Called BottomApp"
@app.call(env)
end
end
class MyApp
def self.call(env)
puts "In the main app"
[200, {'Content-Type' => 'text/html'}, ["My Response"]]
end
end
use TopApp
use MiddleApp
use BottomApp
run MyApp
require 'sinatra/base'
class MyMiddleware < Sinatra::Base
get '/secret' do
status, header, body = app.call(env)
body.first.gsub!('ALL', 'SOME')
end
end
class MyApp < Sinatra::Base
get '/secret' do
"ALL THE SECRETS"
end
end
map '/admin' do
use MyMiddleware
run MyApp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment