Repository URL to install this package:
|
Version:
3.0.2 ▾
|
# frozen_string_literal: true
require 'logger'
require 'serverless_rack'
LOGGER = Logger.new $stdout
module Sinatra
# Helpers for production - using AWS Cognito
module LambdaHandler
module_function
def handler(event:, context:, multi_value_headers: nil, config: {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# The new handle_request function deals with multi_value_headers automatically, so we can deprecate the argument
warn '`multi_value_headers` passed to `handler` is deprecated' unless multi_value_headers.nil?
environ = build_environ(event:, context:, headers: parse_headers(event), body: parse_body(event))
LOGGER.info(environ.slice(*%w[REQUEST_METHOD PATH_INFO QUERY_STRING HTTP_X_FORWARDED_FOR]))
handle_request(app: $app, event:, context:, config:).tap do |response| # rubocop:disable Style/GlobalVars
LOGGER.info "Response code: #{response['statusCode']}"
end
rescue Exception => e # rubocop:disable Lint/RescueException
# If there is _any_ exception, we return a 500 error with an error message
response = { 'statusCode' => 500, 'body' => e.message }
LOGGER.fatal response
LOGGER.fatal e.backtrace.join("\n\t") if e.backtrace
response
end
end
end