Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty-sinatra-aws / lib / sinatra / lambda_handler.rb
Size: Mime:
# 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