Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 81e126c

Browse files
Merge pull request #32 from Andrea-Scuderi/feature/Redis
Feature/redis
2 parents a7825dc + 29bdd1e commit 81e126c

21 files changed

+507
-5
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ script:
5050
- make invoke_lambda_local SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject
5151
- make stop_docker_compose_env SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject
5252
- make test_lambda_local_output SWIFT_EXECUTABLE=S3Test SWIFT_PROJECT_PATH=Examples/S3Test LAMBDA_FUNCTION_NAME=S3Test LAMBDA_HANDLER=S3Test.getObject
53+
- make build_lambda_local SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
54+
- make start_docker_compose_env SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
55+
- make invoke_lambda_local SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
56+
- make stop_docker_compose_env SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
57+
- make test_lambda_local_output SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
5358

5459
deploy:
5560
provider: releases

Examples/HTTPSRequest/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ start_docker_compose_env:
55
cd $(SWIFT_PROJECT_PATH); docker-compose up -d; sleep 5
66

77
stop_docker_compose_env:
8-
cd $(SWIFT_PROJECT_PATH); sleep 5; docker-compose stop
8+
cd $(SWIFT_PROJECT_PATH); docker-compose stop

Examples/HTTPSRequest/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
- ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated
99
environment:
1010
- DOCKER_LAMBDA_STAY_OPEN=1
11-
- AWS_LAMBDA_FUNCTION_TIMEOUT=3
11+
- AWS_LAMBDA_FUNCTION_TIMEOUT=10
1212
- LAMB_CI_EXEC=1
1313
command:
1414
- HTTPSRequest.getHttps

Examples/HelloWorld/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
- ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated
99
environment:
1010
- DOCKER_LAMBDA_STAY_OPEN=1
11-
- AWS_LAMBDA_FUNCTION_TIMEOUT=3
11+
- AWS_LAMBDA_FUNCTION_TIMEOUT=10
1212
- LAMB_CI_EXEC=1
1313
command:
1414
- HelloWorld.helloWorld

Examples/RedisDemo/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.build
3+
/Packages
4+
*.xcodeproj

Examples/RedisDemo/.swiftlint.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
disabled_rules: # rule identifiers to exclude from running
2+
- superfluous_disable_command
3+
- trailing_comma
4+
# - colon
5+
# - comma
6+
# - control_statement
7+
#opt_in_rules: # some rules are only opt-in
8+
# - empty_count
9+
# - missing_docs
10+
# Find all the available rules by running:
11+
# swiftlint rules
12+
#included: # paths to include during linting. `--path` is ignored if present.
13+
# - Source
14+
excluded: # paths to ignore during linting. Takes precedence over `included`.
15+
- Carthage
16+
- Pods
17+
- .build
18+
# configurable rules can be customized from this configuration file
19+
# binary rules can set their severity level
20+
force_cast: warning # implicitly
21+
force_try:
22+
severity: warning # explicitly
23+
# rules that have both warning and error levels, can set just the warning level
24+
# implicitly
25+
line_length: 150
26+
# they can set both implicitly with an array
27+
#type_body_length:
28+
# - 300 # warning
29+
# - 400 # error
30+
# or they can set both explicitly
31+
#file_length:
32+
# warning: 500
33+
# error: 1200
34+
# naming rules can set warnings/errors for min_length and max_length
35+
# additionally they can set excluded names
36+
#type_name#
37+
# min_length: 4 # only warning
38+
# max_length: # warning and error
39+
# warning: 40
40+
# error: 50
41+
# excluded: iPhone # excluded via string
42+
#variable_name:
43+
# min_length: # only min_length
44+
# error: 4 # only error
45+
# excluded: # excluded via string array
46+
# - id
47+
# - URL
48+
# - GlobalAPIKey
49+
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle)

Examples/RedisDemo/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Note: This must be launched from the root path
2+
SWIFT_PROJECT_PATH=Examples/RedisDemo
3+
4+
start_docker_compose_env:
5+
cd $(SWIFT_PROJECT_PATH); docker-compose up -d; sleep 5
6+
7+
stop_docker_compose_env:
8+
cd $(SWIFT_PROJECT_PATH); docker-compose stop

Examples/RedisDemo/Package.resolved

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/RedisDemo/Package.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.1
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "RedisDemo",
8+
dependencies: [
9+
// Dependencies declare other packages that this package depends on.
10+
// .package(url: /* package url */, from: "1.0.0"),
11+
.package(url: "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin", from: "1.0.0-alpha.3"),
12+
.package(url: "https://gitlab.com/mordil/swift-redi-stack.git", from: "1.0.0-alpha.5"),
13+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
14+
],
15+
targets: [
16+
// Targets are the basic building blocks of a package. A starget can define a module or a test suite.
17+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
18+
.target(
19+
name: "RedisDemo",
20+
dependencies: ["LambdaSwiftSprinterNioPlugin", "Logging", "RediStack"]),
21+
.testTarget(
22+
name: "RedisDemoTests",
23+
dependencies: ["RedisDemo"]),
24+
]
25+
)

Examples/RedisDemo/README.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# RedisDemo
2+
3+
[![Swift 5](https://img.shields.io/badge/Swift-5.0-blue.svg)](https://swift.org/download/) [![Swift 5.1.3](https://img.shields.io/badge/Swift-5.1.3-blue.svg)](https://swift.org/download/)
4+
5+
This example shows the usage of the [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core) framework and the plugin [LambdaSwiftSprinterNioPlugin](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin) to build a lambda capable to perform an Redis request using
6+
[RediStack](https://gitlab.com/mordil/swift-redi-stack.git).
7+
8+
## Swift code
9+
10+
Define an Event and a Response as Codable.
11+
```swift
12+
import AsyncHTTPClient
13+
import Foundation
14+
#if canImport(FoundationNetworking)
15+
import FoundationNetworking
16+
#endif
17+
import LambdaSwiftSprinter
18+
import LambdaSwiftSprinterNioPlugin
19+
import Logging
20+
import NIO
21+
import NIOFoundationCompat
22+
import RediStack
23+
24+
struct Event: Codable {
25+
let key: String
26+
let value: String
27+
}
28+
29+
struct Response: Codable {
30+
let value: String
31+
}
32+
```
33+
34+
35+
36+
Add a loger:
37+
```swift
38+
let logger = Logger(label: "AWS.Lambda.Redis")
39+
```
40+
41+
Add a redis connection:
42+
```swift
43+
let elasticacheConfigEndpoint = "redis"
44+
45+
let eventLoop = httpClient.eventLoopGroup.next()
46+
let connection = try? RedisConnection.connect(
47+
to: try .makeAddressResolvingHost(elasticacheConfigEndpoint,
48+
port: RedisConnection.defaultPort),
49+
on: eventLoop
50+
).wait()
51+
52+
53+
54+
enum LambdaError: Error {
55+
case redisConnectionFailed
56+
}
57+
```
58+
59+
Define the Lambda:
60+
```swift
61+
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
62+
63+
guard let connection = connection else {
64+
throw LambdaError.redisConnectionFailed
65+
}
66+
67+
let future = connection.set(event.key, to: event.value)
68+
.flatMap {
69+
return connection.get(event.key)
70+
}
71+
.map { content -> Response in
72+
return Response(value: content ?? "")
73+
}
74+
return future
75+
}
76+
```
77+
78+
If there are not error, the Event will be automatically decoded inside the lambda and then used to perform a `set` and a `get` to Redis.
79+
The value of the `get` is returned into the Response.
80+
81+
Then use this boilerplate code to run the lambda:
82+
```swift
83+
do {
84+
85+
let sprinter = try SprinterNIO()
86+
sprinter.register(handler: "setGet", lambda: syncCodableNIOLambda)
87+
88+
try sprinter.run()
89+
} catch {
90+
logger.error("\(String(describing: error))")
91+
}
92+
```
93+
94+
This will initialize the Sprinter with a Sprinter based on NIO 2 library.
95+
96+
Then the internal handler `setGet` is being registered.
97+
98+
Finally the sprinter run.
99+
100+
Any error will be logged.
101+
102+
Note
103+
104+
In this example we used [swift-log](https://github.com/apple/swift-log.git) to log the output.
105+
106+
## Deployment
107+
108+
To build this example make sure the following lines on the `Makefile` are not commented and the other example configurations are commented.
109+
110+
```
111+
...
112+
113+
# HelloWorld Example Configuration
114+
# SWIFT_EXECUTABLE=HelloWorld
115+
# SWIFT_PROJECT_PATH=Examples/HelloWorld
116+
# LAMBDA_FUNCTION_NAME=HelloWorld
117+
# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).helloWorld
118+
119+
....
120+
121+
# RedisDemo Example Configuration
122+
SWIFT_EXECUTABLE?=RedisDemo
123+
SWIFT_PROJECT_PATH?=Examples/RedisDemo
124+
LAMBDA_FUNCTION_NAME?=RedisDemo
125+
LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).setGet
126+
127+
...
128+
```
129+
130+
Then follow the main [README](https://github.com/swift-sprinter/aws-lambda-swift-sprinter) to build and test the code.
131+
132+
## Test
133+
134+
The test event is defined in the file `event.json`
135+
```json
136+
{
137+
"key": "language",
138+
"value": "Swift"
139+
}
140+
```
141+
142+
expected response:
143+
144+
```json
145+
{"value":"Swift"}
146+
```
147+
148+
Change it to try different output and error conditions.
149+
150+
# LambdaSwiftSprinter
151+
152+
To know more refer to [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core).

0 commit comments

Comments
 (0)