Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Last active July 5, 2019 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/b178fe4b9910bfcc4ed6d76dc5411feb to your computer and use it in GitHub Desktop.
Save keithrbennett/b178fe4b9910bfcc4ed6d76dc5411feb to your computer and use it in GitHub Desktop.
Illustrates the outer middleware in a Rack script can be a lambda.
# 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