Skip to content

Instantly share code, notes, and snippets.

@adamdullenty
Created September 13, 2021 10:44
Show Gist options
  • Save adamdullenty/afa2c0773223d34fcfdd953965aac2fd to your computer and use it in GitHub Desktop.
Save adamdullenty/afa2c0773223d34fcfdd953965aac2fd to your computer and use it in GitHub Desktop.
# MIT No Attribution
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'json'
require 'rack'
require 'base64'
# Global object that responds to the call method. Stay outside of the handler
# to take advantage of container reuse
$app ||= Rack::Builder.parse_file("#{File.dirname(__FILE__)}/app/config.ru").first
def handler(event:, context:)
# Check if the body is base64 encoded. If it is, try to decode it
if event["isBase64Encoded"]
body = Base64.decode64(event['body'])
else
body = event['body']
end
puts "The event: #{event.to_json}"
# Rack expects the querystring in plain text, not a hash
querystring = Rack::Utils.build_query(event['queryStringParameters']) if event['queryStringParameters']
path = (event['requestContext'] && event['requestContext']['path']) || ""
path_info = event['path'] || ""
script_name = path.end_with?(path_info) ? path[0...-path_info.size] : ""
# Environment required by Rack (http://www.rubydoc.info/github/rack/rack/file/SPEC)
env = {
"REQUEST_METHOD" => event['httpMethod'],
"SCRIPT_NAME" => script_name,
"PATH_INFO" => path_info,
"QUERY_STRING" => querystring || "",
"SERVER_NAME" => "localhost",
"SERVER_PORT" => (event['headers']['X-Forwarded-Port'] || '443').to_i,
"CONTENT_TYPE" => event['headers']['Content-Type'],
"CONTENT_LENGTH" => event['headers']['Content-Length'],
"rack.version" => Rack::VERSION,
"rack.url_scheme" => event['headers']['X-Forwarded-Proto'] || "https",
"rack.input" => StringIO.new(body || ""),
"rack.errors" => $stderr,
}
# Pass request headers to Rack if they are available
unless event['headers'].nil?
event['headers'].each{ |key, value| env["HTTP_#{key.gsub("-", "_").upcase}"] = value }
end
puts "The env in lambda.rb #{env}"
begin
# Response from Rack must have status, headers and body
status, headers, body = $app.call(env)
# body is an array. We simply combine all the items to a single string
body_content = ""
body.each do |item|
body_content += item.to_s
end
# We return the structure required by AWS API Gateway since we integrate with it
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
puts "The response headers are: #{headers}"
response = {
"statusCode" => status,
"headers" => headers,
"body" => body_content
}
if event["requestContext"].has_key?("elb")
# Required if we use application load balancer instead of API GW
response["isBase64Encoded"] = false
end
rescue Exception => msg
# If there is any exception, we return a 500 error with an error message
response = {
"statusCode" => 500,
"body" => msg
}
end
# By default, the response serializer will call #to_json for us
response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment