Skip to content

Commit 40d0599

Browse files
authored
breakdown examples intro separate targets (#1)
motivation: easier to reason about examples changes: * move each example to its own target * update deployment script to prompt for desired target when multiple targets exist * add code comments on what the example is about * add feature-reach example (CurrencyExchange) to help testing use of Foundation features that require curl and libxml
1 parent e1ee4ea commit 40d0599

File tree

11 files changed

+543
-203
lines changed

11 files changed

+543
-203
lines changed

Package.swift

+25-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,40 @@
33
import PackageDescription
44

55
let package = Package(
6-
name: "my-lambda",
6+
name: "swift-aws-lambda-runtime-samples",
77
platforms: [
88
.macOS(.v10_13),
99
],
1010
products: [
11-
.executable(name: "MyLambda", targets: ["MyLambda"]),
11+
// introductory example
12+
.executable(name: "HelloWorld", targets: ["HelloWorld"]),
13+
// good for benchmarking
14+
.executable(name: "Benchmark", targets: ["Benchmark"]),
15+
// demonstrate different types of error handling
16+
.executable(name: "ErrorHandling", targets: ["ErrorHandling"]),
17+
// demostrate how to integrate with AWS API Gateway
18+
.executable(name: "APIGateway", targets: ["APIGateway"]),
19+
// fully featured example with domain specific business logic
20+
.executable(name: "CurrencyExchange", targets: ["CurrencyExchange"]),
21+
1222
],
1323
dependencies: [
1424
.package(url: "git@github.com:swift-server/swift-aws-lambda-runtime.git", .branch("master")),
1525
],
1626
targets: [
17-
// lambda code is abstracted into a library since we cant have a test target depend on an executable
18-
.target(name: "MyLambda", dependencies: [
27+
.target(name: "HelloWorld", dependencies: [
28+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
29+
]),
30+
.target(name: "Benchmark", dependencies: [
31+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
32+
]),
33+
.target(name: "ErrorHandling", dependencies: [
34+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
35+
]),
36+
.target(name: "APIGateway", dependencies: [
37+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
38+
]),
39+
.target(name: "CurrencyExchange", dependencies: [
1940
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
2041
]),
2142
]

Sources/APIGateway/main.swift

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
import NIO
17+
18+
// MARK: - Run Lambda
19+
20+
Lambda.run(APIGatewayProxyLambda())
21+
22+
// MARK: - Handler, Request and Response
23+
24+
// FIXME: Use proper Event abstractions once added to AWSLambdaRuntime
25+
struct APIGatewayProxyLambda: EventLoopLambdaHandler {
26+
public typealias In = APIGatewayRequest
27+
public typealias Out = APIGatewayResponse
28+
29+
public func handle(context: Lambda.Context, payload: APIGatewayRequest) -> EventLoopFuture<APIGatewayResponse> {
30+
context.logger.debug("hello, api gateway!")
31+
return context.eventLoop.makeSucceededFuture(APIGatewayResponse(statusCode: 200,
32+
headers: nil,
33+
multiValueHeaders: nil,
34+
body: "hello, world!",
35+
isBase64Encoded: false))
36+
}
37+
}
38+
39+
struct APIGatewayRequest: Codable {
40+
let resource: String
41+
let path: String
42+
let httpMethod: String?
43+
let headers: [String: String]?
44+
let multiValueHeaders: [String: [String]]?
45+
let queryStringParameters: [String: String]?
46+
let multiValueQueryStringParameters: [String: [String]]?
47+
let pathParameters: [String: String]?
48+
let stageVariables: [String: String]?
49+
let requestContext: Context?
50+
let body: String?
51+
let isBase64Encoded: Bool?
52+
53+
struct Context: Codable {
54+
let accountId: String?
55+
let resourceId: String?
56+
let stage: String?
57+
let requestId: String?
58+
let identity: Identity?
59+
let resourcePath: String?
60+
let httpMethod: String?
61+
let apiId: String
62+
}
63+
64+
struct Identity: Codable {
65+
let cognitoIdentityPoolId: String?
66+
let accountId: String?
67+
let cognitoIdentityId: String?
68+
let caller: String?
69+
let apiKey: String?
70+
let sourceIp: String?
71+
let cognitoAuthenticationType: String?
72+
let cognitoAuthenticationProvider: String?
73+
let userArn: String?
74+
let userAgent: String?
75+
let user: String?
76+
}
77+
}
78+
79+
struct APIGatewayResponse: Codable {
80+
let statusCode: Int
81+
let headers: [String: String]?
82+
let multiValueHeaders: [String: [String]]?
83+
let body: String?
84+
let isBase64Encoded: Bool?
85+
}

Sources/Benchmark/main.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
import NIO
17+
18+
// If you would like to benchmark Swift's Lambda Runtime,
19+
// use this example which is more performant.
20+
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
21+
// while the closure-based handlers do.
22+
Lambda.run(BenchmarkHandler())
23+
24+
struct BenchmarkHandler: EventLoopLambdaHandler {
25+
typealias In = String
26+
typealias Out = String
27+
28+
func handle(context: Lambda.Context, payload: String) -> EventLoopFuture<String> {
29+
context.eventLoop.makeSucceededFuture("hello, world!")
30+
}
31+
}

0 commit comments

Comments
 (0)