shark-on-lambda 0.0.0 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.gitlab-ci.yml +13 -0
- data/.rubocop.yml +13 -0
- data/.travis.yml +9 -2
- data/README.md +184 -18
- data/Rakefile +2 -0
- data/bin/console +4 -9
- data/changelog.md +17 -0
- data/gems.locked +92 -0
- data/{Gemfile → gems.rb} +2 -1
- data/lib/shark-on-lambda.rb +1 -5
- data/lib/shark_on_lambda.rb +104 -1
- data/lib/shark_on_lambda/api_gateway/base_controller.rb +76 -0
- data/lib/shark_on_lambda/api_gateway/base_handler.rb +82 -0
- data/lib/shark_on_lambda/api_gateway/concerns/http_response_validation.rb +61 -0
- data/lib/shark_on_lambda/api_gateway/errors.rb +49 -0
- data/lib/shark_on_lambda/api_gateway/headers.rb +37 -0
- data/lib/shark_on_lambda/api_gateway/jsonapi_controller.rb +77 -0
- data/lib/shark_on_lambda/api_gateway/jsonapi_parameters.rb +68 -0
- data/lib/shark_on_lambda/api_gateway/jsonapi_renderer.rb +105 -0
- data/lib/shark_on_lambda/api_gateway/parameters.rb +18 -0
- data/lib/shark_on_lambda/api_gateway/query.rb +69 -0
- data/lib/shark_on_lambda/api_gateway/request.rb +148 -0
- data/lib/shark_on_lambda/api_gateway/response.rb +82 -0
- data/lib/shark_on_lambda/api_gateway/serializers/base_error_serializer.rb +20 -0
- data/lib/shark_on_lambda/concerns/filter_actions.rb +82 -0
- data/lib/shark_on_lambda/concerns/resettable_singleton.rb +18 -0
- data/lib/shark_on_lambda/concerns/yaml_config_loader.rb +28 -0
- data/lib/shark_on_lambda/configuration.rb +71 -0
- data/lib/shark_on_lambda/inferrers/name_inferrer.rb +66 -0
- data/lib/shark_on_lambda/inferrers/serializer_inferrer.rb +45 -0
- data/lib/shark_on_lambda/secrets.rb +43 -0
- data/lib/shark_on_lambda/tasks.rb +3 -0
- data/lib/shark_on_lambda/tasks/build.rake +146 -0
- data/lib/{shark-on-lambda → shark_on_lambda}/version.rb +1 -1
- data/shark-on-lambda.gemspec +21 -6
- metadata +158 -20
- data/Gemfile.lock +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44a4ee1f3d1628311fba41801df9dee659ebae05ad000992310f345248c26a16
|
4
|
+
data.tar.gz: 5b807fc9fe412d64b23c2eee28dd8462f7375302f6a71b89c99e2b023ebbb90a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e0d9987dce6c080ef81c9802df11c29c7e020a23a04e60c70b8c024add57ff8facead26ff82a8e6ead6b7029b3b491162aea23ac31d8ec1cbfbbbad45c186e6
|
7
|
+
data.tar.gz: 2d95aee2c8d0a82a8c53adeea24a77756f1e63f731653265983b56956105b00f9096e480747778daed50dd7ef1066a276d702d7de0e9662842e9a3a609129592
|
data/.gitignore
CHANGED
data/.gitlab-ci.yml
ADDED
data/.rubocop.yml
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
|
4
|
+
Metrics/BlockLength:
|
5
|
+
Exclude:
|
6
|
+
- 'spec/**/*_spec.rb'
|
7
|
+
- 'spec/factories/*.rb'
|
8
|
+
|
1
9
|
Naming/FileName:
|
2
10
|
Exclude:
|
3
11
|
- lib/shark-on-lambda.rb
|
12
|
+
- spec/shark-on-lambda_spec.rb
|
13
|
+
|
14
|
+
# TODO: Add documentation and remove the Style/Documentation exception.
|
15
|
+
Style/Documentation:
|
16
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,43 +1,209 @@
|
|
1
|
-
#
|
1
|
+
# shark-on-lambda
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/Skudo/shark-on-lambda)
|
4
|
+
[](https://codeclimate.com/github/Skudo/shark-on-lambda/maintainability)
|
4
5
|
|
5
|
-
|
6
|
+
`shark-on-lambda` provides a lightweight framework to write Ruby on AWS Lambda
|
7
|
+
whilst using a familiar approach to web services known from e. g. Ruby on Rails.
|
8
|
+
|
9
|
+
## History
|
10
|
+
|
11
|
+
For a long time, "going serverless" on AWS Lambda was a world Ruby developers
|
12
|
+
only could explore using methods such as packing their own Ruby binaries,
|
13
|
+
going the JRuby way, running on the Java VM, or using Ruby On Jets.
|
14
|
+
|
15
|
+
When AWS went public with the Ruby 2.5 runtime for AWS Lambda at the end of
|
16
|
+
2018, that changed for Ruby developers and suddenly, a whole new world opened.
|
17
|
+
|
18
|
+
Since it is possible to run any Rack-based application on AWS Lambda, you can
|
19
|
+
even run your own Rails application there, if you want to.
|
20
|
+
|
21
|
+
However, if you prefer a more lightweight solution, `shark-on-lambda` may be
|
22
|
+
of interest to you, as it offers you a similar approach to things whilst
|
23
|
+
maintaining a smaller memory footprint.
|
24
|
+
|
25
|
+
## Changelog
|
26
|
+
|
27
|
+
### Pre-1.0
|
28
|
+
* Supports events from the API Gateway.
|
6
29
|
|
7
30
|
## Installation
|
8
31
|
|
9
|
-
Add this line to your application's
|
32
|
+
Add this line to your application's `gems.rb`:
|
10
33
|
|
11
34
|
```ruby
|
12
|
-
gem '
|
35
|
+
gem 'shark_on_lambda'
|
13
36
|
```
|
14
37
|
|
15
38
|
And then execute:
|
16
39
|
|
17
40
|
$ bundle
|
18
41
|
|
19
|
-
Or install it yourself
|
42
|
+
Or install it yourself:
|
20
43
|
|
21
|
-
$ gem install
|
44
|
+
$ gem install shark_on_lambda
|
22
45
|
|
23
|
-
##
|
46
|
+
## Handlers
|
24
47
|
|
25
|
-
|
48
|
+
Handlers are the entry points for Lambda function invocation, e. g. when they
|
49
|
+
are triggered by HTTP requests on the API Gateway. They also are responsible for
|
50
|
+
responding with a well-formed response to the caller.
|
26
51
|
|
27
|
-
|
52
|
+
```ruby
|
53
|
+
class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
|
54
|
+
end
|
55
|
+
```
|
28
56
|
|
29
|
-
|
57
|
+
By inheriting from `SharkOnLambda::ApiGateway::BaseHandler`, your own handler
|
58
|
+
class is indirectly tied to your controller class by convention: It assumes
|
59
|
+
the controller name is `MyController` and it will dispatch events to that
|
60
|
+
controller.
|
30
61
|
|
31
|
-
|
62
|
+
If you however bring your own class with a different name, you can configure
|
63
|
+
your handler to use that controller instead:
|
32
64
|
|
33
|
-
|
65
|
+
```ruby
|
66
|
+
class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
|
67
|
+
self.controller_class = AnotherController
|
68
|
+
end
|
69
|
+
```
|
34
70
|
|
35
|
-
|
71
|
+
All controller methods are going to be automagically mapped to class methods
|
72
|
+
on the handler class.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class MyController < SharkOnLambda::ApiGateway::BaseController
|
76
|
+
def index
|
77
|
+
end
|
78
|
+
|
79
|
+
def show
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class MyHandler < SharkOnLambda::ApiGateway::BaseHandler
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
`MyHandler` will respond to `.index` and `.show` class methods that accept the
|
88
|
+
`event` and `context` objects from the API Gateway. Those are passed to
|
89
|
+
`MyController` and eventually, the controller method that corresponds to the
|
90
|
+
handler class method is called.
|
91
|
+
|
92
|
+
## Controllers
|
93
|
+
|
94
|
+
Controllers are similar to Rails controllers: In addition to having access to
|
95
|
+
the AWS Lambda `event` and `context` objects, you also have access to `params`,
|
96
|
+
`request`, and `response` objects that are derived from the `event` object.
|
97
|
+
|
98
|
+
### "Basic" controllers
|
99
|
+
|
100
|
+
You also have access to the `render` and `redirect_to` methods __that are not as
|
101
|
+
powerful as the Rails equivalent by far__. There are none of the `render_*`
|
102
|
+
functions you may know from Rails and `render` does not really support multiple
|
103
|
+
renderers (yet).
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class MyController < SharkOnLambda::ApiGateway::BaseController
|
107
|
+
def index
|
108
|
+
# Make the API Gateway respond with a 201 response saying "Hello, World!"
|
109
|
+
# in the response body.
|
110
|
+
#
|
111
|
+
# The default status code for `render` is 200.
|
112
|
+
render 'Hello, World!', status: 201
|
113
|
+
end
|
114
|
+
|
115
|
+
def show
|
116
|
+
# Does what you think it does.
|
117
|
+
#
|
118
|
+
# The default status code for `redirect_to` is 307.
|
119
|
+
redirect_to 'https://github.com', status: 302
|
120
|
+
end
|
121
|
+
end
|
122
|
+
```
|
36
123
|
|
37
|
-
|
124
|
+
### _JSON API_-compliant controllers
|
38
125
|
|
39
|
-
|
126
|
+
If you inherit your controller from
|
127
|
+
`SharkOnLambda::ApiGateway::JsonapiController`, `render` and `redirect_to` will
|
128
|
+
create _JSON API_-compliant responses.
|
129
|
+
|
130
|
+
You however __must__ have a serialiser for the objects you want to render.
|
131
|
+
Otherwise, rendering will fail and you will receive an _Internal Server Error_
|
132
|
+
instead.
|
133
|
+
|
134
|
+
_JSON API_ `fields` and `include` query string parameters are automagically
|
135
|
+
being parsed and used for rendering automagically by the _JSON API_ renderer.
|
136
|
+
|
137
|
+
## _JSON API_ serialisers
|
138
|
+
|
139
|
+
We use `jsonapi-serializable` (and `jsonapi-rb` in general) for our
|
140
|
+
_JSON API_ compatibility. Therefore, we expect the serialisers to be inherited
|
141
|
+
from `::JSONAPI::Serializable::Resource` (or `::JSONAPI::Serializable::Error`).
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
class SomethingSerializer < ::JSONAPI::Serializable::Resource
|
145
|
+
type :somethings
|
146
|
+
|
147
|
+
attributes :foo, :bar, :baz
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
### Serialiser lookup
|
152
|
+
|
153
|
+
Each object that is to be serialised requires a serialiser class that knows
|
154
|
+
how to serialise it. We also implemented a convention over configuration
|
155
|
+
approach here to determine which serialiser class to use:
|
156
|
+
|
157
|
+
1) If the object is an instance of `YourClass` and `YourClassSerializer` is
|
158
|
+
defined, `YourClassSerializer` is used as the serialiser class.
|
159
|
+
|
160
|
+
2) If the object is an instance of `YourClass` and `YourClassSerializer` is
|
161
|
+
__not__ defined, check whether 1) applies for any of the ancestors of
|
162
|
+
`YourClass`.
|
163
|
+
|
164
|
+
#### Example
|
165
|
+
|
166
|
+
If `YourClass` has `YourBaseClass`, `Object`, and `BasicObject` as ancestor
|
167
|
+
classes, the first existing one of `YourClassSerializer`,
|
168
|
+
`YourBaseClassSerializer`, `ObjectSerializer`, and `BasicObjectSerializer`
|
169
|
+
(in that order) is used. If none of those exist, serialisation will fail with
|
170
|
+
an _Internal Server Error_.
|
171
|
+
|
172
|
+
## Configuration
|
173
|
+
|
174
|
+
You can "initialise" `SharkOnLambda` using its `.initialize!` class method.
|
175
|
+
|
176
|
+
`SharkOnLambda.initialize!` yields to a block with the `config` and `secrets`
|
177
|
+
objects where you can access and add to those two objects.
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
SharkOnLambda.initialize! do |config, secrets|
|
181
|
+
# Do things here.
|
182
|
+
end
|
183
|
+
```
|
184
|
+
|
185
|
+
Calling `SharkOnLambda.initialize!` does these things (in order):
|
186
|
+
|
187
|
+
1. Process the block passed to `.initialize!`.
|
188
|
+
2. Load `config/settings.yml` and `config/settings.local.yml` into the
|
189
|
+
`config` object.
|
190
|
+
3. Load `config/database.yml` and `config/database.local.yml` into
|
191
|
+
`config.database`.
|
192
|
+
4. Load `config/secrets.yml` and `secrets/secrets.local.yml` into the
|
193
|
+
`secrets` object.
|
194
|
+
5. Load all `config/initializers/*.rb` files.
|
195
|
+
|
196
|
+
If `SharkOnLambda.config.stage` was set inside the block passed to
|
197
|
+
`.initialize!`, configurations and secrets for that stage will be merged into
|
198
|
+
the default set (the `default` node in the YAML files) of configuration and
|
199
|
+
secrets, overwriting values where applicable.
|
200
|
+
|
201
|
+
## Development
|
40
202
|
|
41
|
-
|
203
|
+
Clone this repository and change away. Then, once you are done, please submit
|
204
|
+
a pull request at https://github.com/Skudo/shark-on-lambda/pulls.
|
42
205
|
|
43
|
-
|
206
|
+
However, please make sure the tests (`bundle exec rake spec`) and `rubocop`
|
207
|
+
(`bundle exec rubocop`) pass before submitting a pull request. Pull requests
|
208
|
+
that do not pass both on the CI system will not be merged. On the same note,
|
209
|
+
untested code will not be merged, either.
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# frozen_string_literal: true
|
2
4
|
|
3
5
|
require 'bundler/setup'
|
4
|
-
require 'shark-on-lambda'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
6
|
require 'irb'
|
7
|
+
require 'shark_on_lambda'
|
8
|
+
|
14
9
|
IRB.start(__FILE__)
|
data/changelog.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
## Changelog
|
2
|
+
|
3
|
+
#### 0.6.7
|
4
|
+
|
5
|
+
- [Fix] Build 0.6.6 using the wrong branch.
|
6
|
+
|
7
|
+
#### 0.6.6
|
8
|
+
|
9
|
+
- [Fix] Handle the quirkiness of API Gateway query strings properly.
|
10
|
+
|
11
|
+
#### 0.6.5
|
12
|
+
|
13
|
+
- [Fix] Parse nested query string parameters correctly.
|
14
|
+
|
15
|
+
#### 0.6.4
|
16
|
+
|
17
|
+
- Initial public release.
|
data/gems.locked
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
shark-on-lambda (0.6.7)
|
5
|
+
activesupport
|
6
|
+
jsonapi-rb
|
7
|
+
rack
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (6.0.0)
|
13
|
+
activesupport (= 6.0.0)
|
14
|
+
activesupport (6.0.0)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 0.7, < 2)
|
17
|
+
minitest (~> 5.1)
|
18
|
+
tzinfo (~> 1.1)
|
19
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
20
|
+
ast (2.4.0)
|
21
|
+
concurrent-ruby (1.1.5)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
docile (1.3.2)
|
24
|
+
factory_bot (5.1.0)
|
25
|
+
activesupport (>= 4.2.0)
|
26
|
+
i18n (1.6.0)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
jaro_winkler (1.5.3)
|
29
|
+
json (2.2.0)
|
30
|
+
jsonapi-deserializable (0.2.0)
|
31
|
+
jsonapi-rb (0.5.0)
|
32
|
+
jsonapi-deserializable (~> 0.2.0)
|
33
|
+
jsonapi-serializable (~> 0.3.0)
|
34
|
+
jsonapi-renderer (0.2.2)
|
35
|
+
jsonapi-serializable (0.3.1)
|
36
|
+
jsonapi-renderer (~> 0.2.0)
|
37
|
+
minitest (5.12.0)
|
38
|
+
parallel (1.17.0)
|
39
|
+
parser (2.6.4.1)
|
40
|
+
ast (~> 2.4.0)
|
41
|
+
rack (2.0.7)
|
42
|
+
rainbow (3.0.0)
|
43
|
+
rake (13.0.0)
|
44
|
+
rspec (3.8.0)
|
45
|
+
rspec-core (~> 3.8.0)
|
46
|
+
rspec-expectations (~> 3.8.0)
|
47
|
+
rspec-mocks (~> 3.8.0)
|
48
|
+
rspec-core (3.8.2)
|
49
|
+
rspec-support (~> 3.8.0)
|
50
|
+
rspec-expectations (3.8.4)
|
51
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
+
rspec-support (~> 3.8.0)
|
53
|
+
rspec-mocks (3.8.1)
|
54
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
+
rspec-support (~> 3.8.0)
|
56
|
+
rspec-support (3.8.2)
|
57
|
+
rubocop (0.74.0)
|
58
|
+
jaro_winkler (~> 1.5.1)
|
59
|
+
parallel (~> 1.10)
|
60
|
+
parser (>= 2.6)
|
61
|
+
rainbow (>= 2.2.2, < 4.0)
|
62
|
+
ruby-progressbar (~> 1.7)
|
63
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
64
|
+
ruby-progressbar (1.10.1)
|
65
|
+
simplecov (0.17.1)
|
66
|
+
docile (~> 1.1)
|
67
|
+
json (>= 1.8, < 3)
|
68
|
+
simplecov-html (~> 0.10.0)
|
69
|
+
simplecov-html (0.10.2)
|
70
|
+
thread_safe (0.3.6)
|
71
|
+
tzinfo (1.2.5)
|
72
|
+
thread_safe (~> 0.1)
|
73
|
+
unicode-display_width (1.6.0)
|
74
|
+
yard (0.9.20)
|
75
|
+
zeitwerk (2.1.10)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
ruby
|
79
|
+
|
80
|
+
DEPENDENCIES
|
81
|
+
activemodel
|
82
|
+
bundler
|
83
|
+
factory_bot
|
84
|
+
rake
|
85
|
+
rspec
|
86
|
+
rubocop
|
87
|
+
shark-on-lambda!
|
88
|
+
simplecov
|
89
|
+
yard
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
2.0.2
|
data/{Gemfile → gems.rb}
RENAMED