Skip to content

Commit dc3daaf

Browse files
committed
Restructure Examples folder
1 parent 6227444 commit dc3daaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+482
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ xcuserdata
99
Package.resolved
1010
.serverless
1111
.vscode
12+
Makefile

Examples/APIGateway/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
samconfig.toml
2+
Makefile

Examples/APIGateway/Package.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
// needed for CI to test the local version of the library
6+
import class Foundation.ProcessInfo
7+
import struct Foundation.URL
8+
9+
#if os(macOS)
10+
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
11+
#else
12+
let platforms: [PackageDescription.SupportedPlatform]? = nil
13+
#endif
14+
15+
let package = Package(
16+
name: "swift-aws-lambda-runtime-example",
17+
platforms: platforms,
18+
products: [
19+
.executable(name: "APIGAtewayLambda", targets: ["APIGAtewayLambda"])
20+
],
21+
dependencies: [
22+
// dependency on swift-aws-lambda-runtime is added dynamically below
23+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
24+
25+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", branch: "main")
26+
],
27+
targets: [
28+
.executableTarget(
29+
name: "APIGAtewayLambda",
30+
dependencies: [
31+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
32+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
33+
],
34+
path: "."
35+
)
36+
]
37+
)
38+
39+
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"],
40+
localDepsPath != "",
41+
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]),
42+
let _ = v.isDirectory
43+
{
44+
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
45+
package.dependencies += [
46+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
47+
]
48+
49+
} else {
50+
print("[INFO] LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
51+
print("[INFO] This project will compile against the main branch of the Lambda Runtime on GitHub")
52+
package.dependencies += [
53+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
54+
]
55+
}

Examples/APIGateway/README.md

Lines changed: 1 addition & 0 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2024 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 AWSLambdaEvents
16+
import AWSLambdaRuntime
17+
import class Foundation.JSONEncoder
18+
19+
let runtime = LambdaRuntime {
20+
(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in
21+
22+
var header = HTTPHeaders()
23+
context.logger.debug("HTTP API Message received")
24+
25+
header["content-type"] = "application/json"
26+
27+
// echo the request in the response
28+
let data = try JSONEncoder().encode(event)
29+
let response = String(data: data, encoding: .utf8)
30+
31+
return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
32+
}
33+
34+
try await runtime.run()

Examples/APIGateway/template.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: SAM Template for QuoteService
4+
5+
Resources:
6+
# Lambda function
7+
APIGAtewayLambda:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
CodeUri: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGAtewayLambda/APIGAtewayLambda.zip
11+
Timeout: 60
12+
Handler: swift.bootstrap
13+
Runtime: provided.al2
14+
MemorySize: 512
15+
Architectures:
16+
- arm64
17+
Environment:
18+
Variables:
19+
# by default, AWS Lambda runtime produces no log
20+
# use `LOG_LEVEL: debug` for for lifecycle and event handling information
21+
# use `LOG_LEVEL: trace` for detailed input event information
22+
LOG_LEVEL: trace
23+
Events:
24+
HttpApiEvent:
25+
Type: HttpApi
26+
27+
Outputs:
28+
# print API Gateway endpoint
29+
APIGAtewayEndpoint:
30+
Description: API Gateway endpoint UR"
31+
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com"

Examples/Benchmark/Package.swift

Lines changed: 0 additions & 34 deletions
This file was deleted.

Examples/HelloWorld/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
response.json
2+
samconfig.toml
3+
template.yaml
4+
Makefile

Examples/HelloWorld/Package.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
#if os(macOS)
6+
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)]
7+
#else
8+
let platforms: [PackageDescription.SupportedPlatform]? = nil
9+
#endif
10+
11+
// needed for CI to test the local version of the library
12+
import class Foundation.ProcessInfo
13+
import struct Foundation.URL
14+
15+
let package = Package(
16+
name: "swift-aws-lambda-runtime-example",
17+
platforms: platforms,
18+
products: [
19+
.executable(name: "MyLambda", targets: ["MyLambda"])
20+
],
21+
dependencies: [
22+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main")
23+
],
24+
targets: [
25+
.executableTarget(
26+
name: "MyLambda",
27+
dependencies: [
28+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime")
29+
],
30+
path: "."
31+
)
32+
]
33+
)
34+
35+
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" {
36+
37+
print("++++++++ \(localDepsPath)")
38+
39+
// check if directory exists
40+
let u = URL(fileURLWithPath: localDepsPath)
41+
if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! {
42+
print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
43+
package.dependencies = [
44+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
45+
]
46+
} else {
47+
print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
48+
print("This project will compile against the main branch of the Lambda Runtime on GitHub")
49+
}
50+
} else {
51+
print("++++++++ NO ENV VAR ")
52+
53+
}

Examples/HelloWorld/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2024 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+
17+
// in this example we are receiving and responding with strings
18+
19+
let runtime = LambdaRuntime { (event: String, context: LambdaContext) in
20+
return "Hello \(event)"
21+
}
22+
23+
try await runtime.run()

Examples/v1/Benchmark/Package.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// swift-tools-version:5.7
2+
3+
import PackageDescription
4+
5+
// needed for CI to test the local version of the library
6+
import class Foundation.ProcessInfo
7+
import struct Foundation.URL
8+
9+
let runtimeVersion = Version("1.0.0-alpha.3")
10+
11+
let package = Package(
12+
name: "swift-aws-lambda-runtime-example",
13+
platforms: [
14+
.macOS(.v12)
15+
],
16+
products: [
17+
.executable(name: "MyLambda", targets: ["MyLambda"])
18+
],
19+
dependencies: [
20+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: runtimeVersion)
21+
],
22+
targets: [
23+
.executableTarget(
24+
name: "MyLambda",
25+
dependencies: [
26+
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime")
27+
],
28+
path: "."
29+
)
30+
]
31+
)
32+
33+
// for CI to test the local version of the library
34+
// if ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"] != nil {
35+
// print("LAMBDA_USE_LOCAL_DEPS is ignored for runtime v1 examples.")
36+
// print("This project will compile against runtime version \(runtimeVersion)")
37+
// }
38+
39+
if let localDepsPath = ProcessInfo.processInfo.environment["LAMBDA_USE_LOCAL_DEPS"], localDepsPath != "" {
40+
41+
// check if directory exists
42+
let u = URL(fileURLWithPath: localDepsPath)
43+
if let v = try? u.resourceValues(forKeys: [.isDirectoryKey]), v.isDirectory! {
44+
print("Compiling against swift-aws-lambda-runtime located at \(localDepsPath)")
45+
package.dependencies = [
46+
.package(name: "swift-aws-lambda-runtime", path: localDepsPath)
47+
]
48+
} else {
49+
print("LAMBDA_USE_LOCAL_DEPS is not pointing to your local swift-aws-lambda-runtime code")
50+
print("This project will compile against runtime version \(runtimeVersion)")
51+
}
52+
53+
}
File renamed without changes.

Examples/Deployment/Package.swift renamed to Examples/v1/Deployment/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
// demonstrate different types of error handling
1818
],
1919
dependencies: [
20-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha")
20+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3")
2121
],
2222
targets: [
2323
.executableTarget(
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)