Skip to content

Instantly share code, notes, and snippets.

@ericodes
Forked from anonymous/config.ru
Created June 26, 2014 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericodes/ef708790a5678557b278 to your computer and use it in GitHub Desktop.
Save ericodes/ef708790a5678557b278 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