Last active
January 14, 2016 22:46
-
-
Save bfaloona/5c14e9157d935a08000d to your computer and use it in GitHub Desktop.
Several simple Rack Applications and Middlewares
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
use Rack::ContentLength | |
run Proc.new { |env| | |
[200, {}, ['Hello from Rack']] | |
} |
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
# This is Rack Middleware! | |
class PoweredByRack | |
POWERED_BY_HEADER = "X-Powered-By" | |
POWERED_BY_VALUE = "Rack (Ruby Webserver Interface) v#{Rack.release}" | |
def initialize(app, header_value=nil) | |
@app = app | |
@header_value = header_value || POWERED_BY_VALUE | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
headers[POWERED_BY_HEADER] = @header_value | |
[status, headers, body] | |
end | |
end | |
# invoke with either: | |
# use PoweredByRack | |
# use PoweredByRack, "Rack Powered!" |
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
class Hello | |
def self.call(env) | |
request = Rack::Request.new(env) | |
# Evaluate request.accept_language | |
# Return language appropriate body | |
end | |
end | |
run Hello |
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
class Hello | |
def self.call(env) | |
response = Rack::Response.new | |
response['Content-Type'] = 'text/plain' | |
response.status = 200 | |
response.body = ['Hello', ' from Rack!'] | |
response.finish | |
end | |
end | |
run Hello |
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
run Proc.new { |env| | |
[200, {}, ['Hello from Rack']] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment