-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.rb
52 lines (46 loc) · 1.59 KB
/
lambda.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'json'
require 'rack'
$app ||= Rack::Builder.parse_file("#{File.dirname(__FILE__)}/config.ru").first
def handler(event:, context:)
# Environment required by Rack (http://www.rubydoc.info/github/rack/rack/file/SPEC)
env = {
'REQUEST_METHOD' => event['httpMethod'],
'SCRIPT_NAME' => '',
'PATH_INFO' => event['path'] || '',
'QUERY_STRING' => event['queryStringParameters'] || '',
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => 443,
'rack.version' => Rack::VERSION,
'rack.url_scheme' => 'https',
'rack.input' => StringIO.new(event['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}"] = value }
end
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
response = {
'statusCode' => status,
'headers' => headers,
'body' => body_content
}
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