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

Commit 503c4f1

Browse files
Merge pull request #33 from Andrea-Scuderi/feature/PostgreSQLDemo
Feature/postgre sql demo
2 parents 81e126c + 3c89a4a commit 503c4f1

16 files changed

+489
-0
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ script:
5555
- make invoke_lambda_local SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
5656
- make stop_docker_compose_env SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
5757
- make test_lambda_local_output SWIFT_EXECUTABLE=RedisDemo SWIFT_PROJECT_PATH=Examples/RedisDemo LAMBDA_FUNCTION_NAME=RedisDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).setGet
58+
- make build_lambda_local SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
59+
- make start_docker_compose_env SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
60+
- make invoke_lambda_local SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
61+
- make stop_docker_compose_env SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
62+
- make test_lambda_local_output SWIFT_EXECUTABLE=PostgreSQLDemo SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo LAMBDA_FUNCTION_NAME=PostgreSQLDemo LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
5863

5964
deploy:
6065
provider: releases

Examples/PostgreSQLDemo/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.build
3+
/Packages
4+
*.xcodeproj
+49
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/PostgreSQLDemo/Makefile

+8
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/PostgreSQLDemo
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
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"object": {
3+
"pins": [
4+
{
5+
"package": "async-http-client",
6+
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
7+
"state": {
8+
"branch": null,
9+
"revision": "48e284d1ea6d0e8baac1af1c4ad8bd298670caf6",
10+
"version": "1.0.1"
11+
}
12+
},
13+
{
14+
"package": "LambdaSwiftSprinter",
15+
"repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core",
16+
"state": {
17+
"branch": null,
18+
"revision": "3afe37ba73f50cf5735c82a9b389d981cefe483d",
19+
"version": "1.0.0-alpha3"
20+
}
21+
},
22+
{
23+
"package": "LambdaSwiftSprinterNioPlugin",
24+
"repositoryURL": "https://github.com/swift-sprinter/aws-lambda-swift-sprinter-nio-plugin",
25+
"state": {
26+
"branch": null,
27+
"revision": "6b11518ec8e8c4f7eb968120d17f026f9449e2c9",
28+
"version": "1.0.0-alpha.3"
29+
}
30+
},
31+
{
32+
"package": "postgres-nio",
33+
"repositoryURL": "https://github.com/vapor/postgres-nio.git",
34+
"state": {
35+
"branch": null,
36+
"revision": "3adaee5fa31630d7f37fb818c2668c562bc7cdbb",
37+
"version": "1.0.0-beta.2.4"
38+
}
39+
},
40+
{
41+
"package": "swift-log",
42+
"repositoryURL": "https://github.com/apple/swift-log.git",
43+
"state": {
44+
"branch": null,
45+
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
46+
"version": "1.2.0"
47+
}
48+
},
49+
{
50+
"package": "swift-metrics",
51+
"repositoryURL": "https://github.com/apple/swift-metrics.git",
52+
"state": {
53+
"branch": null,
54+
"revision": "3fefedaaef285830cc98ae80231140122076a7e0",
55+
"version": "1.2.0"
56+
}
57+
},
58+
{
59+
"package": "swift-nio",
60+
"repositoryURL": "https://github.com/apple/swift-nio.git",
61+
"state": {
62+
"branch": null,
63+
"revision": "f6487a11d80bfb9a0a0a752b7442847c7e3a8253",
64+
"version": "2.12.0"
65+
}
66+
},
67+
{
68+
"package": "swift-nio-extras",
69+
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
70+
"state": {
71+
"branch": null,
72+
"revision": "53808818c2015c45247cad74dc05c7a032c96a2f",
73+
"version": "1.3.2"
74+
}
75+
},
76+
{
77+
"package": "swift-nio-ssl",
78+
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
79+
"state": {
80+
"branch": null,
81+
"revision": "b75ffaba05b2cffdb1420d558f1a90b4e6c46dcc",
82+
"version": "2.5.0"
83+
}
84+
}
85+
]
86+
},
87+
"version": 1
88+
}

Examples/PostgreSQLDemo/Package.swift

+25
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: "PostgreSQLDemo",
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://github.com/vapor/postgres-nio.git", from: "1.0.0-alpha.1.6"),
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 target 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: "PostgreSQLDemo",
20+
dependencies: ["LambdaSwiftSprinterNioPlugin", "Logging", "PostgresNIO"]),
21+
.testTarget(
22+
name: "PostgreSQLDemoTests",
23+
dependencies: ["PostgreSQLDemo"]),
24+
]
25+
)

Examples/PostgreSQLDemo/README.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# PostgreSQLDemo
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 Postgres query using
6+
[PostgresNIO](https://github.com/vapor/postgres-nio.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 PostgresNIO
23+
24+
struct Event: Codable {
25+
let query: String
26+
}
27+
28+
struct Response: Codable {
29+
let value: String
30+
}
31+
```
32+
33+
34+
35+
Add a loger:
36+
```swift
37+
let logger = Logger(label: "AWS.Lambda.Postgres")
38+
```
39+
40+
Add a redis connection, define the Lambda and run it:
41+
```swift
42+
enum LambdaError: Error {
43+
case connectionFailed
44+
}
45+
46+
//let endpoint = "<yourdb>.rds.amazonaws.com"
47+
let endpoint = "postgres"
48+
49+
do {
50+
let eventLoop = httpClient.eventLoopGroup.next()
51+
let connection = try PostgresConnection.connect(
52+
to: try .makeAddressResolvingHost(endpoint,
53+
port: 5432),
54+
on: eventLoop
55+
).wait()
56+
57+
logger.error("after connection")
58+
59+
try connection.authenticate(username: "username1",
60+
database: "demoDB",
61+
password: "password1").wait()
62+
63+
64+
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
65+
66+
let future = connection.query(event.query).map { (rows) -> Response in
67+
return Response(value: "\(rows)")
68+
69+
}
70+
return future
71+
}
72+
73+
74+
let sprinter = try SprinterNIO()
75+
sprinter.register(handler: "query", lambda: syncCodableNIOLambda)
76+
77+
try sprinter.run()
78+
} catch {
79+
logger.error("\(String(describing: error))")
80+
}
81+
```
82+
83+
If there are not error, the Event will be automatically decoded inside the lambda and then used to perform a `query` to Postgres.
84+
The result of the `query` is returned into the Response.
85+
86+
87+
Note
88+
89+
In this example we used [swift-log](https://github.com/apple/swift-log.git) to log the output.
90+
91+
## Deployment
92+
93+
To build this example make sure the following lines on the `Makefile` are not commented and the other example configurations are commented.
94+
95+
```
96+
...
97+
98+
# HelloWorld Example Configuration
99+
# SWIFT_EXECUTABLE=HelloWorld
100+
# SWIFT_PROJECT_PATH=Examples/HelloWorld
101+
# LAMBDA_FUNCTION_NAME=HelloWorld
102+
# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).helloWorld
103+
104+
....
105+
106+
# PostgreSQLDemo Example Configuration
107+
SWIFT_EXECUTABLE=PostgreSQLDemo
108+
SWIFT_PROJECT_PATH=Examples/PostgreSQLDemo
109+
LAMBDA_FUNCTION_NAME=PostgreSQLDemo
110+
LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).query
111+
112+
...
113+
```
114+
115+
Then follow the main [README](https://github.com/swift-sprinter/aws-lambda-swift-sprinter) to build and test the code.
116+
117+
## Test
118+
119+
The test event is defined in the file `event.json`
120+
```json
121+
{
122+
"query": "SELECT 1+1 as result;"
123+
}
124+
```
125+
126+
expected response:
127+
128+
```json
129+
{"value":"[[\"result\": 2]]"}
130+
```
131+
132+
Change it to try different output and error conditions.
133+
134+
# LambdaSwiftSprinter
135+
136+
To know more refer to [LambdaSwiftSprinter](https://github.com/swift-sprinter/aws-lambda-swift-sprinter-core).

0 commit comments

Comments
 (0)