Skip to content

Commit 762b2aa

Browse files
committed
add the tutorial example code
1 parent 410f0b8 commit 762b2aa

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Examples/Tutorial/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/Tutorial/Package.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// swift-tools-version: 6.0
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: "Palindrome",
8+
platforms: [ .macOS(.v15)],
9+
dependencies: [
10+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
11+
],
12+
targets: [
13+
// Targets are the basic building blocks of a package, defining a module or a test suite.
14+
// Targets can depend on other targets in this package and products from dependencies.
15+
.executableTarget(
16+
name: "Palindrome",
17+
dependencies: [
18+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
19+
]),
20+
]
21+
)
22+
23+
import struct Foundation.URL
24+
25+
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"],
26+
localDepsPath != "",
27+
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
28+
v.isDirectory == true
29+
{
30+
// when we use the local runtime as deps, let's remove the dependency added above
31+
let indexToRemove = package.dependencies.firstIndex { dependency in
32+
if case .sourceControl(
33+
name: _,
34+
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
35+
requirement: _
36+
) = dependency.kind {
37+
return true
38+
}
39+
return false
40+
}
41+
if let indexToRemove {
42+
package.dependencies.remove(at: indexToRemove)
43+
}
44+
45+
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..)
46+
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
47+
package.dependencies += [
48+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
49+
]
50+
}

Examples/Tutorial/Sources/main.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import AWSLambdaRuntime
2+
3+
// the data structure to represent the input parameter
4+
struct Request: Decodable {
5+
let text: String
6+
}
7+
8+
// the data structure to represent the response parameter
9+
struct Response: Encodable {
10+
let text: String
11+
let isPalindrome: Bool
12+
let message: String
13+
}
14+
15+
// the business function
16+
func isPalindrome(_ text: String) -> Bool {
17+
let cleanedText = text.lowercased().filter { $0.isLetter }
18+
return cleanedText == String(cleanedText.reversed())
19+
}
20+
21+
// the lambda handler function
22+
let runtime = LambdaRuntime {
23+
(event: Request, context: LambdaContext) -> Response in
24+
25+
let result = isPalindrome(event.text)
26+
return Response(text: event.text, isPalindrome: result, message: "Your text is \(result ? "a" : "not a") palindrome")
27+
}
28+
29+
// start the runtime
30+
try await runtime.run()

0 commit comments

Comments
 (0)