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

Commit 3c89a4a

Browse files
Add TravicCI to PostgreSQLDemo example
1 parent bf084af commit 3c89a4a

File tree

11 files changed

+242
-15
lines changed

11 files changed

+242
-15
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
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

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
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/PostgreSQLDemo/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/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

Examples/PostgreSQLDemo/Package.resolved

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

Examples/PostgreSQLDemo/README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,136 @@
11
# PostgreSQLDemo
22

3-
A description of this package.
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).

Examples/PostgreSQLDemo/Sources/PostgreSQLDemo/main.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ enum LambdaError: Error {
3737
case connectionFailed
3838
}
3939

40-
let logger = Logger(label: "AWS.Lambda.Redis")
41-
let endpoint = "<yourdb>.rds.amazonaws.com"
40+
let logger = Logger(label: "AWS.Lambda.Postgres")
41+
42+
//let endpoint = "<yourdb>.rds.amazonaws.com"
43+
let endpoint = "postgres"
44+
4245
do {
4346
let eventLoop = httpClient.eventLoopGroup.next()
4447
let connection = try PostgresConnection.connect(
@@ -49,9 +52,9 @@ do {
4952

5053
logger.error("after connection")
5154

52-
try connection.authenticate(username: "<username>",
53-
database: "<db>",
54-
password: "<password>").wait()
55+
try connection.authenticate(username: "username1",
56+
database: "demoDB",
57+
password: "password1").wait()
5558

5659

5760
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3'
2+
services:
3+
lambci:
4+
image: "lambci/lambda:provided"
5+
volumes:
6+
- ../../.build/local:/var/task:ro,delegated
7+
- ../../bootstrap:/opt/bootstrap:ro,delegated
8+
- ../../swift-shared-libs:/opt/swift-shared-libs:ro,delegated
9+
environment:
10+
- DOCKER_LAMBDA_STAY_OPEN=1
11+
- AWS_LAMBDA_FUNCTION_TIMEOUT=10
12+
- LAMB_CI_EXEC=1
13+
command:
14+
- PostgreSQLDemo.query
15+
ports:
16+
- "9001:9001"
17+
postgres:
18+
image: postgres
19+
restart: always
20+
environment:
21+
- POSTGRES_USER=username1
22+
- POSTGRES_PASSWORD=password1
23+
- POSTGRES_DB=demoDB

Examples/PostgreSQLDemo/event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"query": "SELECT version()"
2+
"query": "SELECT 1+1 as result;"
33
}

Examples/PostgreSQLDemo/outfile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"value":"[[\"result\": 2]]"}

0 commit comments

Comments
 (0)