Permalink
Browse files

Allow both ALB and API GW

  • Loading branch information...
xiangshen-dk committed Dec 12, 2018
1 parent 8004c40 commit d625fae3b0ba07b7f9c8a3b2f47020f4e1a17152
Showing with 24 additions and 11 deletions.
  1. +1 −0 Gemfile
  2. +3 −1 Gemfile.lock
  3. +1 −1 app/server.rb
  4. +3 −3 app/views/feedback.erb
  5. +2 −2 app/views/index.erb
  6. +14 −4 lambda.rb
@@ -6,6 +6,7 @@ gem 'rack'
gem 'rack-contrib'
gem 'aws-record'
gem 'rake'
gem 'rubysl-base64'

# These are the dependencies that are used only for unit tests.
group :test do
@@ -39,6 +39,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubysl-base64 (2.0.0)
sinatra (2.0.4)
mustermann (~> 1.0)
rack (~> 2.0)
@@ -57,7 +58,8 @@ DEPENDENCIES
rack-test
rake
rspec
rubysl-base64
sinatra

BUNDLED WITH
1.16.6
1.17.2
@@ -63,5 +63,5 @@ class FeedbackServerlessSinatraTable
item.feedback = params[:feedback]
item.save! # raise an exception if save fails

item.to_json
item.to_h.to_json
end
@@ -25,7 +25,7 @@
fdata["feedback"] = $("#feedback").val();
$.ajax({
cache: false,
url : "/Prod/api/feedback",
url : "api/feedback",
type: "POST",
dataType : "json",
data : JSON.stringify(fdata),
@@ -46,7 +46,7 @@
function getAllItems() {
$.ajax({
url: "/Prod/api/feedback"
url: "api/feedback"
})
.done(function(json) {
$.each(json, function(i) {
@@ -74,7 +74,7 @@
<h3>Hope you enjoying programming with AWS Lambda! Please share your feedback or any random thoughts.</h3>
</div>

<form action="/Prod/feedback" method="post" id="feedbackForm">
<form action="feedback" method="post" id="feedbackForm">
<div class="form-group">
<label for="name">Name:</label>
<input class="form-control" id="name">
@@ -4,5 +4,5 @@
</h1>
<div>- from SINATRA on AWS Lambda</div>
<br>
<div>Also checkout the API example: <a href="/Prod/hello-world">Hello World</a></div>
<div>And a web app example: <a href="/Prod/feedback">Feedback</a></div>
<div>Also checkout the API example: <a href="hello-world">Hello World</a></div>
<div>And a web app example: <a href="feedback">Feedback</a></div>
@@ -15,12 +15,19 @@

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
# Environment required by Rack (http://www.rubydoc.info/github/rack/rack/file/SPEC)
env = {
"REQUEST_METHOD" => event['httpMethod'],
@@ -30,17 +37,17 @@ def handler(event:, context:)
"SERVER_NAME" => "localhost",
"SERVER_PORT" => 443,
"CONTENT_TYPE" => event['headers']['content-type'],

"rack.version" => Rack::VERSION,
"rack.url_scheme" => "https",
"rack.input" => StringIO.new(event['body'] || ""),
"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}"] = value }
end

begin
# Response from Rack must have status, headers and body
status, headers, body = $app.call(env)
@@ -55,10 +62,13 @@ def handler(event:, context:)
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
response = {
"statusCode" => status,
"isBase64Encoded" => false,
"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 = {

0 comments on commit d625fae

Please sign in to comment.