Created
March 11, 2016 21:04
-
-
Save adaedra/ad2fe4e35db98e3fee04 to your computer and use it in GitHub Desktop.
Rack full example
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
Application = proc do |env| | |
names = env['names'] || %w[world] | |
text = "Hello, #{names.join ', '}!" | |
[200, {'Content-Type' => 'text/plain'}, [text]] | |
end | |
class NameMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
names = env['REQUEST_PATH'].split('/').reject(&:empty?) | |
env['names'] = names unless names.empty? | |
@app.call env | |
end | |
end | |
class Blackhole | |
def initialize(app, regex) | |
@app = app | |
@regex = regex | |
end | |
def call(env) | |
if @regex =~ env['REQUEST_PATH'] | |
[404, {'Content-Type' => 'text/plain'}, ['Not found']] | |
else | |
@app.call env | |
end | |
end | |
end | |
use Blackhole, %r{\A/favicon\.ico} | |
use NameMiddleware | |
run Application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment