yake 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -12
- data/lib/yake/api.rb +6 -0
- data/lib/yake/dsl.rb +4 -5
- data/lib/yake/logger.rb +27 -19
- data/lib/yake/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2be2a80d8b7737cf954f33faef88eb7054ce7b0bbb516f8a216f3b6ec0b5578c
|
4
|
+
data.tar.gz: 69e7b3323594c0f2d794674dc938104ecf1f2d37ccbd4818272aefb6ab4345f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 206fdff33a178be14611721811bf5d3177b7e5b3bc7bf09237844aeac2a93e74b7b88281675a169fe41333dfaa9d40eb8bfb4aab3cac20ee762c3fa4d419c5d0
|
7
|
+
data.tar.gz: cc3a7a75a4f467028acc9bc892681067fbe86f4be6ed7cfe120deadfd34100f2da648f013d6ac81021fade9bcf0342ba576b6fa61a520cbc0724022aadae0aba
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ require "yake/api"
|
|
26
26
|
|
27
27
|
header "content-type" => "application/json"
|
28
28
|
|
29
|
-
get "/fizz" do
|
29
|
+
get "/fizz" do
|
30
30
|
respond 200, { ok: true }.to_json
|
31
31
|
end
|
32
32
|
|
@@ -59,19 +59,17 @@ Or install it yourself as:
|
|
59
59
|
gem install yake
|
60
60
|
```
|
61
61
|
|
62
|
-
## Why
|
62
|
+
## Why Is It Called "yake"?
|
63
63
|
|
64
|
-
|
64
|
+
"λ" + Rake, but "λ" is hard to type and I think "y" looks like a funny little upside-down-and-backwards Lambda symbol.
|
65
65
|
|
66
|
-
|
66
|
+
## Why Use It?
|
67
67
|
|
68
|
-
`yake`
|
68
|
+
So why use `yake` for your Lambda functions?
|
69
69
|
|
70
70
|
#### Event Logging
|
71
71
|
|
72
|
-
By default, the `handler` function wraps its block in log lines formatted to match the style of Amazon's native Lambda logs sent to CloudWatch. Each invocation of the handler
|
73
|
-
|
74
|
-
Example log lines:
|
72
|
+
By default, the `handler` function wraps its block in log lines formatted to match the style of Amazon's native Lambda logs sent to CloudWatch. Each invocation of the handler will log both the _input event_ and the _returned value_, prefixed with the ID of the request:
|
75
73
|
|
76
74
|
```
|
77
75
|
START RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Version: $LATEST
|
@@ -82,14 +80,25 @@ END RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf
|
|
82
80
|
REPORT RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Duration: 43.97 ms Billed Duration: 44 ms Memory Size: 128 MB Max Memory Used: 77 MB
|
83
81
|
```
|
84
82
|
|
85
|
-
|
83
|
+
Logging the request ID in this way makes gathering logs lines for a particular execution in CloudWatch much easier.
|
86
84
|
|
87
|
-
This feature can be disabled by adding a declaration in your handler:
|
85
|
+
This feature can be disabled by adding a declaration in your handler file:
|
88
86
|
|
89
87
|
```ruby
|
90
88
|
logging :off
|
91
89
|
```
|
92
90
|
|
91
|
+
Include `Yake::Logger` on a class to access this logger:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
class Fizz
|
95
|
+
include Yake::Logger
|
96
|
+
end
|
97
|
+
|
98
|
+
Fizz.new.logger == Yake.logger
|
99
|
+
# => true
|
100
|
+
```
|
101
|
+
|
93
102
|
#### API Routes
|
94
103
|
|
95
104
|
A common use of Lambda functions is as a proxy for API Gateway. Oftentimes users will deploy a single Lambda function to handle all requests coming from API Gateway.
|
@@ -144,7 +153,7 @@ respond 200, { ok: true }.to_json, "x-extra-header" => "buzz"
|
|
144
153
|
# {
|
145
154
|
# "statusCode" => 200,
|
146
155
|
# "body" => '{"ok":true}',
|
147
|
-
# "headers" => { "x-extra-header" => "
|
156
|
+
# "headers" => { "x-extra-header" => "buzz" }
|
148
157
|
# }
|
149
158
|
```
|
150
159
|
|
@@ -156,10 +165,14 @@ handler :lambda_handler do |event|
|
|
156
165
|
rescue Yake::UndeclaredRoute => err
|
157
166
|
respond 404, { message: err.message }.to_json
|
158
167
|
rescue => err
|
159
|
-
respond 500 { message: err.message }.to_json
|
168
|
+
respond 500, { message: err.message }.to_json
|
160
169
|
end
|
161
170
|
```
|
162
171
|
|
172
|
+
#### Zero Dependencies
|
173
|
+
|
174
|
+
Finally, `yake` does not depend on any other gems, using the Ruby stdlib only. This helps keep your Lambda packages slim & speedy.
|
175
|
+
|
163
176
|
## Development
|
164
177
|
|
165
178
|
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.
|
data/lib/yake/api.rb
CHANGED
@@ -55,6 +55,12 @@ module Yake
|
|
55
55
|
(@headers ||= {}).update(headers)
|
56
56
|
end
|
57
57
|
|
58
|
+
##
|
59
|
+
# Define ANY route
|
60
|
+
def any(path, &block)
|
61
|
+
define_singleton_method("ANY #{ path }") { |*args| instance_exec(*args, &block) }
|
62
|
+
end
|
63
|
+
|
58
64
|
##
|
59
65
|
# Define DELETE route
|
60
66
|
def delete(path, &block)
|
data/lib/yake/dsl.rb
CHANGED
@@ -10,17 +10,17 @@ module Yake
|
|
10
10
|
# Lambda handler task wrapper
|
11
11
|
def handler(name, &block)
|
12
12
|
define_method(name) do |event:nil, context:nil|
|
13
|
-
Yake.
|
13
|
+
Yake.wrap(event, context, &block)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
##
|
18
18
|
# Turn logging on/off
|
19
|
-
def logging(switch,
|
19
|
+
def logging(switch, logger = nil)
|
20
20
|
if switch == :on
|
21
|
-
Yake.logger =
|
21
|
+
Yake.logger = logger
|
22
22
|
elsif switch == :off
|
23
|
-
Yake.logger = nil
|
23
|
+
Yake.logger = ::Logger.new(nil)
|
24
24
|
else
|
25
25
|
raise Errors::UnknownLoggingSetting, switch
|
26
26
|
end
|
@@ -29,4 +29,3 @@ module Yake
|
|
29
29
|
end
|
30
30
|
|
31
31
|
extend Yake::DSL
|
32
|
-
Yake.logger = Yake::Logger.new
|
data/lib/yake/logger.rb
CHANGED
@@ -4,33 +4,41 @@ require "json"
|
|
4
4
|
require "logger"
|
5
5
|
|
6
6
|
module Yake
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
7
|
+
module Logger
|
8
|
+
attr_accessor :logger
|
9
|
+
|
10
|
+
def logger
|
11
|
+
@logger ||= Yake.logger
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
ensure
|
19
|
-
@progname = "-"
|
14
|
+
class << self
|
15
|
+
def new(logdev = $stdout)
|
16
|
+
::Logger.new(logdev, progname: "-", formatter: Formatter.new)
|
17
|
+
end
|
20
18
|
end
|
21
|
-
end
|
22
19
|
|
23
|
-
|
24
|
-
|
20
|
+
class Formatter < ::Logger::Formatter
|
21
|
+
Format = "%s %s %s\n"
|
25
22
|
|
26
|
-
|
27
|
-
|
23
|
+
def call(severity, time, progname, msg)
|
24
|
+
Format % [ severity, progname, msg2str(msg).strip ]
|
25
|
+
end
|
28
26
|
end
|
29
27
|
end
|
30
28
|
|
31
|
-
|
29
|
+
class << self
|
32
30
|
attr_accessor :logger
|
33
|
-
end
|
34
31
|
|
35
|
-
|
32
|
+
def logger
|
33
|
+
@logger ||= Logger.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def wrap(event = nil, context = nil, &block)
|
37
|
+
logger.progname = "RequestId: #{ context.aws_request_id }" if context.respond_to?(:aws_request_id)
|
38
|
+
logger.info("EVENT #{ event.to_json }")
|
39
|
+
yield(event, context).tap { |res| logger.info("RETURN #{ res.to_json }") }
|
40
|
+
ensure
|
41
|
+
logger.progname = "-"
|
42
|
+
end
|
43
|
+
end
|
36
44
|
end
|
data/lib/yake/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yake
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Mancevice
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|