-
Notifications
You must be signed in to change notification settings - Fork 0
/
alb_simulate.rb
89 lines (73 loc) · 1.74 KB
/
alb_simulate.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'sinatra/base'
require 'json'
require 'httpclient'
require 'base64'
require 'uri'
# ruby app.rb -o 0.0.0.0
set :port, 8091
set :bind, '0.0.0.0'
def lambda_process(event)
cli = HTTPClient.new
url = "#{ENV.fetch('LAMBDA_DOCKER_HOST', nil)}/2015-03-31/functions/function/invocations"
resp = cli.post(url, event)
body = JSON.parse(resp.body)
status body['statusCode']
headers body['headers']
body['body']
end
def lambda_process_assets(event)
# Prevent resource contention by inserting random delay
sleep(6 * rand)
lambda_process(event)
end
get '/lambda*' do
path = params['splat'][0]
path = path.gsub(%r{^lambda/}, '')
# remove leading slash, if present
path = path.gsub(%r{^/}, '')
event = {
path: path,
queryStringParameters: params,
httpMethod: 'GET'
}.to_json
lambda_process(event)
end
get '/web/*' do
path = params['splat'][0]
event = {
path: "/web/#{path}",
queryStringParameters: params,
httpMethod: 'GET'
}.to_json
lambda_process_assets(event)
end
post '/lambda*' do
path = params['splat'][0]
path = path.gsub(%r{^lambda/}, '')
# remove leading slash, if present
path = path.gsub(%r{^/}, '')
event = {
path: path,
body: Base64.encode64(URI.encode_www_form(params)),
httpMethod: 'POST',
isBase64Encoded: true
}.to_json
lambda_process(event)
end
post '/web/*' do
path = params['splat'][0]
path = path.gsub(%r{^lambda/}, '')
# remove leading slash, if present
path = path.gsub(%r{^/}, '')
event = {
path: path,
body: Base64.encode64(URI.encode_www_form(params)),
httpMethod: 'POST',
isBase64Encoded: true
}.to_json
lambda_process(event)
end