-
-
Save ericodes/ef708790a5678557b278 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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