-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.rb
More file actions
37 lines (32 loc) · 1000 Bytes
/
lambda_function.rb
File metadata and controls
37 lines (32 loc) · 1000 Bytes
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
require "uri"
require "net/http"
module SmalrubyScratchApiProxyGetProjectInfo
ALLOW_ORIGINS = %w[
https://smalruby.app
https://smalruby.jp
http://localhost:8601
]
API_HOST = "https://api.scratch.mit.edu"
def self.lambda_handler(event:, context:)
origin = event.dig("headers", "origin").to_s.strip
headers = {
"Access-Control-Allow-Origin": ALLOW_ORIGINS.include?(origin) ? origin : ALLOW_ORIGINS.first,
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "OPTIONS,GET"
}
project_id = event.dig("pathParameters", "projectId")
api_uri = URI.join(API_HOST, "/projects/#{project_id}")
res = Net::HTTP.get(api_uri)
{
statusCode: 200,
headers:,
body: res.dup.force_encoding("utf-8")
}
end
end
# AWS Lambda entry point
unless ENV["CI"] == "true"
def lambda_handler(event:, context:)
SmalrubyScratchApiProxyGetProjectInfo.lambda_handler(event: event, context: context)
end
end