The code for https://www.apostergiou.com/rack-sailing/
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 HelloWorldApp | |
def self.call(env) | |
# 200 is the HTTP status code | |
# the second element is the response HTTP header hash | |
# finally the last element is the response body | |
['200', {'Content-Type' => 'text/html'}, ['A hello world rack app.']] | |
end | |
end | |
class AddSomeHeaders | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
@app.call(env).tap { |status, headers, body| headers['X-awesome'] = true } | |
end | |
end | |
class EditSomeHeaders | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
@app.call(env).tap do |status, headers, body| | |
headers.delete('X-awesome') | |
headers['X-double-awesome'] = 'true' | |
end | |
end | |
end | |
app = Rack::Builder.new do | |
use EditSomeHeaders | |
use AddSomeHeaders | |
run HelloWorldApp | |
end | |
Rack::Handler::WEBrick.run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment