1919
2020# Global object that responds to the call method. Stay outside of the handler
2121# to take advantage of container reuse
22- $app ||= Rack ::Builder . parse_file ( "#{ File . dirname ( __FILE__ ) } /app/config.ru" ) . first
22+ $app ||= Rack ::Builder . parse_file ( "#{ __dir__ } /app/config.ru" ) . first
2323
2424def handler ( event :, context :)
2525 # Check if the body is base64 encoded. If it is, try to decode it
26- if event [ "isBase64Encoded" ]
27- body = Base64 . decode64 ( event [ 'body' ] )
28- else
29- body = event [ 'body' ]
30- end
26+ body =
27+ if event [ 'isBase64Encoded' ]
28+ Base64 . decode64 ( event [ 'body' ] )
29+ else
30+ event [ 'body' ]
31+ end
3132 # Rack expects the querystring in plain text, not a hash
3233 querystring = Rack ::Utils . build_query ( event [ 'queryStringParameters' ] ) if event [ 'queryStringParameters' ]
3334 # Environment required by Rack (http://www.rubydoc.info/github/rack/rack/file/SPEC)
3435 env = {
35- " REQUEST_METHOD" => event [ 'httpMethod' ] ,
36- " SCRIPT_NAME" => "" ,
37- " PATH_INFO" => event [ 'path' ] || "" ,
38- " QUERY_STRING" => querystring || "" ,
39- " SERVER_NAME" => " localhost" ,
40- " SERVER_PORT" => 443 ,
41- " CONTENT_TYPE" => event [ 'headers' ] [ 'content-type' ] ,
36+ ' REQUEST_METHOD' => event [ 'httpMethod' ] ,
37+ ' SCRIPT_NAME' => '' ,
38+ ' PATH_INFO' => event [ 'path' ] || '' ,
39+ ' QUERY_STRING' => querystring || '' ,
40+ ' SERVER_NAME' => ' localhost' ,
41+ ' SERVER_PORT' => 443 ,
42+ ' CONTENT_TYPE' => event [ 'headers' ] [ 'content-type' ] ,
4243
43- " rack.version" => Rack ::VERSION ,
44- " rack.url_scheme" => " https" ,
45- " rack.input" => StringIO . new ( body || "" ) ,
46- " rack.errors" => $stderr,
44+ ' rack.version' => Rack ::VERSION ,
45+ ' rack.url_scheme' => ' https' ,
46+ ' rack.input' => StringIO . new ( body || '' ) ,
47+ ' rack.errors' => $stderr,
4748 }
4849 # Pass request headers to Rack if they are available
4950 unless event [ 'headers' ] . nil?
50- event [ 'headers' ] . each { |key , value | env [ "HTTP_#{ key } " ] = value }
51+ event [ 'headers' ] . each { |key , value | env [ "HTTP_#{ key } " ] = value }
5152 end
5253
5354 begin
5455 # Response from Rack must have status, headers and body
5556 status , headers , body = $app. call ( env )
5657
57- # body is an array. We simply combine all the items to a single string
58+ # body is an array. We combine all the items to a single string
5859 body_content = ""
5960 body . each do |item |
6061 body_content += item . to_s
@@ -63,19 +64,19 @@ def handler(event:, context:)
6364 # We return the structure required by AWS API Gateway since we integrate with it
6465 # https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
6566 response = {
66- " statusCode" => status ,
67- " headers" => headers ,
68- " body" => body_content
67+ ' statusCode' => status ,
68+ ' headers' => headers ,
69+ ' body' => body_content
6970 }
70- if event [ " requestContext" ] . has_key? ( " elb" )
71- # Required if we use application load balancer instead of API GW
72- response [ " isBase64Encoded" ] = false
71+ if event [ ' requestContext' ] . key? ( ' elb' )
72+ # Required if we use Application Load Balancer instead of API Gateway
73+ response [ ' isBase64Encoded' ] = false
7374 end
7475 rescue Exception => msg
7576 # If there is any exception, we return a 500 error with an error message
7677 response = {
77- " statusCode" => 500 ,
78- " body" => msg
78+ ' statusCode' => 500 ,
79+ ' body' => msg
7980 }
8081 end
8182 # By default, the response serializer will call #to_json for us
0 commit comments