apigatewayv2_rack 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/Dockerfile.integration +13 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +8 -0
- data/apigatewayv2_rack.gemspec +37 -0
- data/integration/Gemfile +6 -0
- data/integration/Gemfile.lock +27 -0
- data/integration/Makefile +9 -0
- data/integration/README.md +6 -0
- data/integration/app.rb +19 -0
- data/integration/config.ru +7 -0
- data/integration/main.rb +21 -0
- data/integration/template.jsonnet +26 -0
- data/lib/apigatewayv2_rack/error.rb +3 -0
- data/lib/apigatewayv2_rack/request.rb +116 -0
- data/lib/apigatewayv2_rack/response.rb +90 -0
- data/lib/apigatewayv2_rack/version.rb +5 -0
- data/lib/apigatewayv2_rack.rb +36 -0
- data/sig/apigatewayv2_rack.rbs +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 894ffb241bac0229d4db0ab44c78b9904237561253594e2ba17cd7bd9c14ec36
|
4
|
+
data.tar.gz: 209f5920b8ae62b716251d2f4923f24a5e300dd1ef4808cdf81859af59ebd7d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71e29e5f860810014dc3d9df8487f1357c4662593d08a1f831cba74fb46f01dec83752b36f79bdfb80e3685b119591f87afd261a0fc7e684a60c541a7e77b160
|
7
|
+
data.tar.gz: 7330a8b7f66b1a6304b11c015916a691b0bf702f50c8d399937de38b2e9765c5807b53a6b4615721f8f437bfa2af38f4b71ff8f3daef83685ee87faf08e6af1d
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
FROM public.ecr.aws/lambda/ruby:2.7
|
2
|
+
|
3
|
+
ENV GEM_HOME=${LAMBDA_TASK_ROOT}
|
4
|
+
ENV BUNDLE_PATH=${LAMBDA_TASK_ROOT}/vendor/bundle
|
5
|
+
COPY integration/Gemfile* ${LAMBDA_TASK_ROOT}/
|
6
|
+
RUN bundle install
|
7
|
+
|
8
|
+
COPY lib ${LAMBDA_TASK_ROOT}/lib
|
9
|
+
COPY integration/* ${LAMBDA_TASK_ROOT}/
|
10
|
+
|
11
|
+
#RUN find ${LAMBDA_TASK_ROOT}
|
12
|
+
|
13
|
+
CMD ["main.Main.handle"]
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
apigatewayv2_rack (0.1.0)
|
5
|
+
rack
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
rack (3.0.0)
|
12
|
+
rake (13.0.6)
|
13
|
+
rspec (3.11.0)
|
14
|
+
rspec-core (~> 3.11.0)
|
15
|
+
rspec-expectations (~> 3.11.0)
|
16
|
+
rspec-mocks (~> 3.11.0)
|
17
|
+
rspec-core (3.11.0)
|
18
|
+
rspec-support (~> 3.11.0)
|
19
|
+
rspec-expectations (3.11.1)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.11.0)
|
22
|
+
rspec-mocks (3.11.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.11.0)
|
25
|
+
rspec-support (3.11.1)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
apigatewayv2_rack!
|
32
|
+
rake (~> 13.0)
|
33
|
+
rspec (~> 3.0)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.3.16
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Sorah Fukumori
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# apigatewayv2_rack: serve Rack app from AWS Lambda function via API Gateway V2 (HTTP API) or ALB (ELB v2) lambda target
|
2
|
+
|
3
|
+
Apigatewayv2Rack provides a method to convert a AWS Lambda invocation event from API Gateway V2 (HTTP API) or ALB lambda target (ELBv2) to a Rack request environment and a method to convert a Rack response tuple to a corresponding Lambda response object.
|
4
|
+
|
5
|
+
This gem also provides support for Lambda function URL as it uses the same schema with API Gateway V2.
|
6
|
+
|
7
|
+
## Supported deployment and limitation
|
8
|
+
|
9
|
+
- Supports Rack 2 and Rack 3 specification
|
10
|
+
- The following AWS Lambda invocation event schemas:
|
11
|
+
- [Amazon API Gateway HTTP API payload version 2.0](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html)
|
12
|
+
- [ALB lambda function target](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html)
|
13
|
+
- Multiple field lines are not supported on API Gateway schema except `set-cookie` header due to API Gateway's limitation
|
14
|
+
- `lambda.multi_value_headers.enabled` is recommended to be set for usage with ALB. <sup>[[doc](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#enable-multi-value-headers)]</sup>
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
### Quick usage
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# Gemfile
|
22
|
+
gem 'apigatewayv2_rack'
|
23
|
+
```
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# main.rb
|
27
|
+
require 'apigatewayv2_rack'
|
28
|
+
Main = Apigatewayv2Rack.handler_from_rack_config_file(File.join(__dir__, 'config.ru'))
|
29
|
+
```
|
30
|
+
|
31
|
+
And set lambda function handler to `main.Main.handle` then voila!
|
32
|
+
|
33
|
+
### Non-quick usage
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
req = Apigatewayv2Rack::Request.new(event: event, context: context)
|
37
|
+
status, headers, body = rack_app.call(req.to_h)
|
38
|
+
resp = Apigatewayv2Rack::Response.new(status: status, headers: headers, body: body, elb: req.elb?, multivalued: req.multivalued?)
|
39
|
+
p resp.as_json
|
40
|
+
```
|
41
|
+
|
42
|
+
### Full example
|
43
|
+
|
44
|
+
See [./Dockerfile.integration](./Dockerfile.integration) and [./integration](./integration).
|
45
|
+
|
46
|
+
## Development
|
47
|
+
|
48
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
49
|
+
|
50
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sorah/apigatewayv2_rack.
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/apigatewayv2_rack/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "apigatewayv2_rack"
|
7
|
+
spec.version = Apigatewayv2Rack::VERSION
|
8
|
+
spec.authors = ["Sorah Fukumori"]
|
9
|
+
spec.email = ["her@sorah.jp"]
|
10
|
+
|
11
|
+
spec.summary = "handle AWS Lambda API Gateway V2 or ALB (ELBv2) lambda request event with Rack application"
|
12
|
+
spec.homepage = "https://github.com/sorah/apigatewayv2_rack"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/sorah/apigatewayv2_rack"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/sorah/apigatewayv2_rack/blob/main/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
spec.add_dependency 'rack'
|
34
|
+
|
35
|
+
# For more information and examples about making a new gem, check out our
|
36
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
37
|
+
end
|
data/integration/Gemfile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
mustermann (3.0.0)
|
5
|
+
ruby2_keywords (~> 0.0.1)
|
6
|
+
rack (2.2.4)
|
7
|
+
rack-protection (3.0.2)
|
8
|
+
rack
|
9
|
+
ruby2_keywords (0.0.5)
|
10
|
+
sinatra (3.0.2)
|
11
|
+
mustermann (~> 3.0)
|
12
|
+
rack (~> 2.2, >= 2.2.4)
|
13
|
+
rack-protection (= 3.0.2)
|
14
|
+
tilt (~> 2.0)
|
15
|
+
tilt (2.0.11)
|
16
|
+
webrick (1.7.0)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
rack (< 3)
|
23
|
+
sinatra
|
24
|
+
webrick
|
25
|
+
|
26
|
+
BUNDLED WITH
|
27
|
+
2.3.16
|
data/integration/app.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
class App < Sinatra::Base
|
4
|
+
get '/' do
|
5
|
+
cnt = session[:cnt] || 0
|
6
|
+
content_type :html
|
7
|
+
"<!DOCTYPE html><html><head><meta charset='utf-8'><title>apigatewayv2_rack test</title><body><p>Hello from Lambda!</p><form method='post'><p><button type='submit'>+</button> #{cnt}</p></form>"
|
8
|
+
end
|
9
|
+
|
10
|
+
post '/' do
|
11
|
+
session[:cnt] ||= 0
|
12
|
+
session[:cnt] += 1
|
13
|
+
redirect '/'
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/errortown' do
|
17
|
+
raise RuntimeError, 'errortown...'
|
18
|
+
end
|
19
|
+
end
|
data/integration/main.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.unshift(File.join(__dir__, 'lib'))
|
2
|
+
require 'rack'
|
3
|
+
require 'rack/builder'
|
4
|
+
require 'apigatewayv2_rack'
|
5
|
+
|
6
|
+
|
7
|
+
#module Main
|
8
|
+
# @app = Rack::Builder.load_file(File.join(__dir__, 'config.ru'), {})[0]
|
9
|
+
# def self.handle(event:, context:)
|
10
|
+
# puts(JSON.generate(event: event, context: context))
|
11
|
+
# retval = Apigatewayv2Rack.handle_request(event: event, context: context, app: @app)
|
12
|
+
# puts(JSON.generate(retval))
|
13
|
+
# retval
|
14
|
+
# end
|
15
|
+
#end
|
16
|
+
#def handler(event:, context:)
|
17
|
+
# Main.handle(event: event, context: context)
|
18
|
+
#end
|
19
|
+
|
20
|
+
$stdout.sync = true
|
21
|
+
Main = Apigatewayv2Rack.handler_from_rack_config_file(File.join(__dir__, 'config.ru'))
|
@@ -0,0 +1,26 @@
|
|
1
|
+
// SAM template
|
2
|
+
{
|
3
|
+
AWSTemplateFormatVersion: '2010-09-09',
|
4
|
+
Transform: 'AWS::Serverless-2016-10-31',
|
5
|
+
Description: '',
|
6
|
+
|
7
|
+
Resources: {
|
8
|
+
AppFunction: {
|
9
|
+
Type: 'AWS::Serverless::Function', // https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
|
10
|
+
Properties: {
|
11
|
+
PackageType: 'Image',
|
12
|
+
FunctionUrlConfig: { AuthType: 'NONE' },
|
13
|
+
},
|
14
|
+
Metadata: {
|
15
|
+
DockerContext: '..',
|
16
|
+
Dockerfile: 'Dockerfile.integration',
|
17
|
+
},
|
18
|
+
},
|
19
|
+
},
|
20
|
+
Outputs: {
|
21
|
+
AppUrl: {
|
22
|
+
Description: 'app endpoint',
|
23
|
+
Value: { 'Fn::GetAtt': ['AppFunctionUrl', 'FunctionUrl'] },
|
24
|
+
},
|
25
|
+
},
|
26
|
+
}
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'uri'
|
3
|
+
require 'base64'
|
4
|
+
require 'rack'
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
require_relative "./error"
|
8
|
+
|
9
|
+
module Apigatewayv2Rack
|
10
|
+
# Converts API Gateway V2 payload format or ALB event format (ELBv2)
|
11
|
+
# https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
12
|
+
# https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
|
13
|
+
class Request
|
14
|
+
def initialize(event, context, use_x_forwarded_host: false)
|
15
|
+
@event = event
|
16
|
+
@context = context
|
17
|
+
|
18
|
+
@use_x_forwarded_host = use_x_forwarded_host
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :event, :context
|
22
|
+
attr_reader :use_x_forwarded_host
|
23
|
+
|
24
|
+
def elb?
|
25
|
+
event.dig('requestContext')&.key?('elb')
|
26
|
+
end
|
27
|
+
|
28
|
+
def multivalued?
|
29
|
+
event.key?('multiValueHeaders')
|
30
|
+
end
|
31
|
+
|
32
|
+
def protocol
|
33
|
+
event.dig('requestContext', 'http', 'protocol') || 'HTTP/1.1'
|
34
|
+
end
|
35
|
+
|
36
|
+
def request_method
|
37
|
+
event['httpMethod'] || event.fetch('requestContext').fetch('http').fetch('method')
|
38
|
+
end
|
39
|
+
|
40
|
+
def path
|
41
|
+
(event['rawPath'] || event['path']) or raise Apigatewayv2Rack::Error.new("neither rawPath and path are defined")
|
42
|
+
end
|
43
|
+
|
44
|
+
def query_string
|
45
|
+
event['rawQueryString'] || encode_query_string_parameters(event['multiValueQueryStringParameters'] || event['queryStringParameters']) || ''
|
46
|
+
end
|
47
|
+
|
48
|
+
private def encode_query_string_parameters(query_string_parameters)
|
49
|
+
# alb queryStringParameters does not decode uri escape, but apigatewayv2 does. However apigatewayv2 has rawQueryString so it should have no problem.
|
50
|
+
query_string_parameters.flat_map { |k,vs| [*vs].map { |v| "#{k}=#{v}" } }.join('&')
|
51
|
+
end
|
52
|
+
|
53
|
+
def body
|
54
|
+
@body ||= event['body'] ? (event['isBase64Encoded'] ? Base64.decode64(event['body']) : event['body']) : ''
|
55
|
+
end
|
56
|
+
|
57
|
+
def source_ip
|
58
|
+
event.dig('requestContext', 'http', 'sourceIp') || '0.0.0.0' # XXX:
|
59
|
+
end
|
60
|
+
|
61
|
+
def headers
|
62
|
+
# Assume everything is lower-cased
|
63
|
+
@headers ||= event['multiValueHeaders']&.transform_values { |v| v.join(',') } || event['headers']
|
64
|
+
end
|
65
|
+
|
66
|
+
def headers_as_env
|
67
|
+
r = {}
|
68
|
+
headers.each do |k,v|
|
69
|
+
next if k == 'content-type'
|
70
|
+
next if k == 'content-length'
|
71
|
+
r["HTTP_#{k.upcase.tr(?-, ?_)}"] = v
|
72
|
+
end
|
73
|
+
r
|
74
|
+
end
|
75
|
+
|
76
|
+
def cookies_as_env
|
77
|
+
if event['cookies']
|
78
|
+
{ 'HTTP_COOKIE' => event['cookies'].join('; ') }
|
79
|
+
else
|
80
|
+
{}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_h
|
85
|
+
{
|
86
|
+
'SERVER_PROTOCOL' => protocol,
|
87
|
+
'REQUEST_METHOD' => request_method,
|
88
|
+
'SCRIPT_NAME' => '',
|
89
|
+
'PATH_INFO' => path,
|
90
|
+
'QUERY_STRING' => query_string,
|
91
|
+
'SERVER_NAME' => headers['host'] || 'unknown',
|
92
|
+
'SERVER_PORT' => (use_x_forwarded_host && ['x-forwarded-port']&.to_i&.to_s) || '80',
|
93
|
+
'CONTENT_LENGTH' => body.bytesize.to_s,
|
94
|
+
'CONTENT_TYPE' => headers['content-type'] || '',
|
95
|
+
'REMOTE_ADDR' => source_ip,
|
96
|
+
'rack.version' => Rack::VERSION,
|
97
|
+
'rack.url_scheme' => (use_x_forwarded_host && headers['x-forwarded-proto']) || 'https',
|
98
|
+
'rack.input' => StringIO.new(body),
|
99
|
+
'rack.errors' => $stderr,
|
100
|
+
'rack.multithread' => false,
|
101
|
+
'rack.multiprocess' => false,
|
102
|
+
'rack.run_once' => false,
|
103
|
+
# compat with serverless-rack gem
|
104
|
+
'serverless.event' => event,
|
105
|
+
'serverless.context' => context,
|
106
|
+
'serverless.authorizer' => nil,
|
107
|
+
# itself
|
108
|
+
'apigatewayv2.request' => self,
|
109
|
+
}
|
110
|
+
.merge(headers_as_env)
|
111
|
+
.merge(cookies_as_env)
|
112
|
+
rescue KeyError => e
|
113
|
+
raise Apigatewayv2Rack::Error.new("malformed request: #{e.inspect}")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'uri'
|
3
|
+
require 'base64'
|
4
|
+
require 'rack'
|
5
|
+
require 'forwardable'
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
require_relative "./error"
|
9
|
+
|
10
|
+
module Apigatewayv2Rack
|
11
|
+
# Convert rack response to API Gateway V2 event response or ALB lambda target response (ELBv2)
|
12
|
+
# https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
|
13
|
+
# https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
|
14
|
+
class Response
|
15
|
+
def initialize(status:, headers:, body:, elb: false, multivalued: false)
|
16
|
+
@status = status
|
17
|
+
@headers = headers
|
18
|
+
@body = body
|
19
|
+
|
20
|
+
@elb = elb
|
21
|
+
@multivalued = multivalued
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :status, :headers, :body
|
25
|
+
|
26
|
+
def elb?
|
27
|
+
@elb
|
28
|
+
end
|
29
|
+
|
30
|
+
def multivalued?
|
31
|
+
@multivalued
|
32
|
+
end
|
33
|
+
|
34
|
+
private def consume_body
|
35
|
+
case
|
36
|
+
when body.respond_to?(:to_ary)
|
37
|
+
body.to_ary.join
|
38
|
+
when body.respond_to?(:each)
|
39
|
+
buf = String.new
|
40
|
+
body.each { |chunk| buf << chunk.b }
|
41
|
+
body.close if body.respond_to?(:close)
|
42
|
+
buf
|
43
|
+
else
|
44
|
+
stream = StringIO.new('', 'w')
|
45
|
+
body.call(stream)
|
46
|
+
stream.string
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private def header_value(v_or_vs)
|
51
|
+
if v_or_vs.kind_of?(Array)
|
52
|
+
v_or_vs
|
53
|
+
else
|
54
|
+
v_or_vs.split(?\n)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def headers_as_response
|
59
|
+
case
|
60
|
+
when elb? && multivalued?
|
61
|
+
{multiValueHeaders: headers.map { |k,v| [k.downcase, header_value(v)] }.to_h}
|
62
|
+
when elb?
|
63
|
+
{headers: headers.map { |k,v| [k.downcase, header_value(v).join(?,)] }.to_h}
|
64
|
+
else
|
65
|
+
{headers: headers.map { |k,v| [k.downcase, header_value(v).join(?,)] }.reject { |k,v| k == 'set-cookie' }.to_h}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def cookies_as_response
|
70
|
+
if elb?
|
71
|
+
{}
|
72
|
+
else
|
73
|
+
hdr = headers.find { |k,_v| k.downcase == 'set-cookie' }
|
74
|
+
return {} unless hdr
|
75
|
+
{ cookies: header_value(hdr[1]) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def as_json
|
80
|
+
{
|
81
|
+
statusCode: status,
|
82
|
+
isBase64Encoded: true,
|
83
|
+
body: Base64.strict_encode64(consume_body),
|
84
|
+
}
|
85
|
+
.merge(headers_as_response)
|
86
|
+
.merge(cookies_as_response)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "apigatewayv2_rack/version"
|
4
|
+
require_relative "apigatewayv2_rack/error"
|
5
|
+
require_relative "apigatewayv2_rack/request"
|
6
|
+
require_relative "apigatewayv2_rack/response"
|
7
|
+
|
8
|
+
module Apigatewayv2Rack
|
9
|
+
# Takes Rack +app+, Lambda +event+ and +context+ of API Gateway V2 event and
|
10
|
+
# returns a HTTP response from +app+ as API Gateway V2 Lambda event format.
|
11
|
+
def self.handle_request(app:, event:, context:, request_options: {})
|
12
|
+
req = Request.new(event, context, **request_options)
|
13
|
+
status, headers, body = app.call(req.to_h)
|
14
|
+
Response.new(status: status, headers: headers, body: body, elb: req.elb?, multivalued: req.multivalued?).as_json
|
15
|
+
end
|
16
|
+
|
17
|
+
module Handler
|
18
|
+
attr_reader :app
|
19
|
+
def handle(event:, context:)
|
20
|
+
Apigatewayv2Rack.handle_request(event: event, context: context, app: @app)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.generate_handler(app)
|
25
|
+
m = Module.new
|
26
|
+
m.extend(Handler)
|
27
|
+
m.instance_variable_set(:@app, app)
|
28
|
+
m
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.handler_from_rack_config_file(path = './config.ru')
|
32
|
+
require 'rack'
|
33
|
+
require 'rack/builder'
|
34
|
+
generate_handler(Rack::Builder.load_file(path, {})[0])
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apigatewayv2_rack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sorah Fukumori
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- her@sorah.jp
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- CHANGELOG.md
|
36
|
+
- Dockerfile.integration
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- apigatewayv2_rack.gemspec
|
43
|
+
- integration/Gemfile
|
44
|
+
- integration/Gemfile.lock
|
45
|
+
- integration/Makefile
|
46
|
+
- integration/README.md
|
47
|
+
- integration/app.rb
|
48
|
+
- integration/config.ru
|
49
|
+
- integration/main.rb
|
50
|
+
- integration/template.jsonnet
|
51
|
+
- lib/apigatewayv2_rack.rb
|
52
|
+
- lib/apigatewayv2_rack/error.rb
|
53
|
+
- lib/apigatewayv2_rack/request.rb
|
54
|
+
- lib/apigatewayv2_rack/response.rb
|
55
|
+
- lib/apigatewayv2_rack/version.rb
|
56
|
+
- sig/apigatewayv2_rack.rbs
|
57
|
+
homepage: https://github.com/sorah/apigatewayv2_rack
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata:
|
61
|
+
homepage_uri: https://github.com/sorah/apigatewayv2_rack
|
62
|
+
source_code_uri: https://github.com/sorah/apigatewayv2_rack
|
63
|
+
changelog_uri: https://github.com/sorah/apigatewayv2_rack/blob/main/CHANGELOG.md
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 2.7.0
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubygems_version: 3.3.7
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: handle AWS Lambda API Gateway V2 or ALB (ELBv2) lambda request event with
|
83
|
+
Rack application
|
84
|
+
test_files: []
|