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

Commit 217625d

Browse files
RedisDemo Example
1 parent a7825dc commit 217625d

File tree

7 files changed

+262
-4
lines changed

7 files changed

+262
-4
lines changed

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+
)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2019 (c) Andrea Scuderi - https://github.com/swift-sprinter
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import AsyncHTTPClient
16+
import Foundation
17+
#if canImport(FoundationNetworking)
18+
import FoundationNetworking
19+
#endif
20+
import LambdaSwiftSprinter
21+
import LambdaSwiftSprinterNioPlugin
22+
import Logging
23+
import NIO
24+
import NIOFoundationCompat
25+
import RediStack
26+
27+
struct Event: Codable {
28+
let key: String
29+
let value: String
30+
}
31+
32+
struct Response: Codable {
33+
let value: String
34+
}
35+
36+
enum LambdaError: Error {
37+
case redisConnectionFailed
38+
}
39+
40+
let logger = Logger(label: "AWS.Lambda.Redis")
41+
let elasticacheConfigEndpoint = "<your cluster>.euw1.cache.amazonaws.com"
42+
43+
let eventLoop = httpClient.eventLoopGroup.next()
44+
let connection = try? RedisConnection.connect(
45+
to: try .makeAddressResolvingHost(elasticacheConfigEndpoint,
46+
port: RedisConnection.defaultPort),
47+
on: eventLoop
48+
).wait()
49+
50+
51+
let syncCodableNIOLambda: SyncCodableNIOLambda<Event, Response> = { (event, context) throws -> EventLoopFuture<Response> in
52+
53+
guard let connection = connection else {
54+
throw LambdaError.redisConnectionFailed
55+
}
56+
57+
let future = connection.set(event.key, to: event.value)
58+
.flatMap {
59+
return connection.get(event.key)
60+
}
61+
.map { content -> Response in
62+
return Response(value: content ?? "")
63+
}
64+
return future
65+
}
66+
67+
do {
68+
69+
let sprinter = try SprinterNIO()
70+
//Note amend this line in case if it's required to use a different lambda handler.
71+
sprinter.register(handler: "setGet", lambda: syncCodableNIOLambda)
72+
73+
try sprinter.run()
74+
} catch {
75+
logger.error("\(String(describing: error))")
76+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import RedisDemoTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += RedisDemoTests.allTests()
7+
XCTMain(tests)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import XCTest
2+
import class Foundation.Bundle
3+
4+
final class RedisDemoTests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
10+
// Some of the APIs that we use below are available in macOS 10.13 and above.
11+
guard #available(macOS 10.13, *) else {
12+
return
13+
}
14+
15+
let fooBinary = productsDirectory.appendingPathComponent("aws-lambda-swift-sprinter-redis")
16+
17+
let process = Process()
18+
process.executableURL = fooBinary
19+
20+
let pipe = Pipe()
21+
process.standardOutput = pipe
22+
23+
try process.run()
24+
process.waitUntilExit()
25+
26+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
27+
let output = String(data: data, encoding: .utf8)
28+
29+
XCTAssertEqual(output, "Hello, world!\n")
30+
}
31+
32+
/// Returns path to the built products directory.
33+
var productsDirectory: URL {
34+
#if os(macOS)
35+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
36+
return bundle.bundleURL.deletingLastPathComponent()
37+
}
38+
fatalError("couldn't find the products directory")
39+
#else
40+
return Bundle.main.bundleURL
41+
#endif
42+
}
43+
44+
static var allTests = [
45+
("testExample", testExample),
46+
]
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(RedisDemoTests.allTests),
7+
]
8+
}
9+
#endif

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ SWIFT_CONFIGURATION=release
1313
# Configuration
1414

1515
# HelloWorld Example Configuration
16-
SWIFT_EXECUTABLE?=HelloWorld
17-
SWIFT_PROJECT_PATH?=Examples/HelloWorld
18-
LAMBDA_FUNCTION_NAME?=HelloWorld
19-
LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).helloWorld
16+
# SWIFT_EXECUTABLE?=HelloWorld
17+
# SWIFT_PROJECT_PATH?=Examples/HelloWorld
18+
# LAMBDA_FUNCTION_NAME?=HelloWorld
19+
# LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).helloWorld
2020

2121
# HTTPSRequest Example Configuration
2222
# SWIFT_EXECUTABLE=HTTPSRequest
@@ -30,6 +30,12 @@ LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).helloWorld
3030
# LAMBDA_FUNCTION_NAME=S3Test
3131
# LAMBDA_HANDLER=$(SWIFT_EXECUTABLE).getObject
3232

33+
# RedisDemo Example Configuration
34+
SWIFT_EXECUTABLE?=RedisDemo
35+
SWIFT_PROJECT_PATH?=Examples/RedisDemo
36+
LAMBDA_FUNCTION_NAME?=RedisDemo
37+
LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).setGet
38+
3339
# AWS Configuration
3440
IAM_ROLE_NAME?=lambda_sprinter_basic_execution
3541
AWS_PROFILE?=default

0 commit comments

Comments
 (0)