Last active
July 5, 2019 22:17
-
-
Save keithrbennett/b178fe4b9910bfcc4ed6d76dc5411feb to your computer and use it in GitHub Desktop.
Illustrates the outer middleware in a Rack script can be a lambda.
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
# Illustrates that a Rack outer app can be a lambda. | |
# This code is stored at https://gist.github.com/keithrbennett/b178fe4b9910bfcc4ed6d76dc5411feb. | |
# See another approach at https://gist.github.com/keithrbennett/08aa675f04353b7c3a9faef2c53cddc8. | |
require "rack" | |
require "thin" | |
# Using a StringIO in case strings will be frozen. | |
BODY = StringIO.new | |
INNER_APP = -> (env) do | |
BODY << "Hello World, from the inner middleware.\n" | |
[ 200, { "Content-Type" => "text/plain" }, BODY ] | |
end | |
OUTER_APP = -> (env) do | |
status, headers, body = INNER_APP.call(env) | |
BODY << "Hello, World, from the outer middleware.\n" | |
[status, headers, BODY.string] | |
end | |
Rack::Handler::Thin.run(OUTER_APP) | |
# Outputs: | |
# | |
# Hello World, from the inner middleware. | |
# Hello, World, from the outer middleware. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment