Skip to content

Instantly share code, notes, and snippets.

@bfaloona
Last active January 14, 2016 22:46
Show Gist options
  • Save bfaloona/5c14e9157d935a08000d to your computer and use it in GitHub Desktop.
Save bfaloona/5c14e9157d935a08000d to your computer and use it in GitHub Desktop.
Several simple Rack Applications and Middlewares
use Rack::ContentLength
run Proc.new { |env|
[200, {}, ['Hello from Rack']]
}
# 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!"
class Hello
def self.call(env)
request = Rack::Request.new(env)
# Evaluate request.accept_language
# Return language appropriate body
end
end
run Hello
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
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