Skip to content

Commit 2768f39

Browse files
authored
Merge branch 'main' into v2-api-proposal
2 parents ca059f8 + 053de48 commit 2768f39

File tree

131 files changed

+3659
-7931
lines changed

Some content is hidden

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

131 files changed

+3659
-7931
lines changed

.github/release.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
changelog:
2+
categories:
3+
- title: SemVer Major
4+
labels:
5+
- ⚠️ semver/major
6+
- title: SemVer Minor
7+
labels:
8+
- semver/minor
9+
- title: SemVer Patch
10+
labels:
11+
- semver/patch
12+
- title: Other Changes
13+
labels:
14+
- semver/none
15+
- "*"

.github/workflows/pull_request.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, reopened, synchronize]
6+
7+
jobs:
8+
soundness:
9+
name: Soundness
10+
uses: apple/swift-nio/.github/workflows/soundness.yml@main
11+
with:
12+
license_header_check_project_name: "SwiftAWSLambdaRuntime"
13+
shell_check_enabled: false
14+
api_breakage_check_container_image: "swift:6.0-noble"
15+
docs_check_container_image: "swift:6.0-noble"
16+
17+
unit-tests:
18+
name: Unit tests
19+
uses: apple/swift-nio/.github/workflows/unit_tests.yml@main
20+
with:
21+
linux_5_8_enabled: false
22+
linux_5_9_enabled: false
23+
linux_5_10_enabled: false
24+
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error"
25+
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"
26+
27+
swift-6-language-mode:
28+
name: Swift 6 Language Mode
29+
uses: apple/swift-nio/.github/workflows/swift_6_language_mode.yml@main

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ xcuserdata
99
Package.resolved
1010
.serverless
1111
.vscode
12+
Makefile

.licenseignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.gitignore
2+
.licenseignore
3+
.swiftformatignore
4+
.spi.yml
5+
.swift-format
6+
.github/*
7+
*.md
8+
**/*.md
9+
CONTRIBUTORS.txt
10+
LICENSE.txt
11+
NOTICE.txt
12+
Package.swift
13+
Package@swift-*.swift
14+
Package.resolved
15+
**/*.docc/*
16+
**/.gitignore
17+
**/Package.swift
18+
**/Package.resolved
19+
**/docker-compose*.yaml
20+
**/docker/*
21+
**/.dockerignore
22+
**/Dockerfile
23+
**/Makefile
24+
**/*.html
25+
**/*-template.yml
26+
**/*.xcworkspace/*
27+
**/*.xcodeproj/*
28+
**/*.xcassets/*
29+
**/*.appiconset/*
30+
**/ResourcePackaging/hello.txt
31+
.mailmap
32+
.swiftformat
33+
*.yaml
34+
*.yml

.swift-format

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"version" : 1,
3+
"indentation" : {
4+
"spaces" : 4
5+
},
6+
"tabWidth" : 4,
7+
"fileScopedDeclarationPrivacy" : {
8+
"accessLevel" : "private"
9+
},
10+
"spacesAroundRangeFormationOperators" : false,
11+
"indentConditionalCompilationBlocks" : false,
12+
"indentSwitchCaseLabels" : false,
13+
"lineBreakAroundMultilineExpressionChainComponents" : false,
14+
"lineBreakBeforeControlFlowKeywords" : false,
15+
"lineBreakBeforeEachArgument" : true,
16+
"lineBreakBeforeEachGenericRequirement" : true,
17+
"lineLength" : 120,
18+
"maximumBlankLines" : 1,
19+
"respectsExistingLineBreaks" : true,
20+
"prioritizeKeepingFunctionOutputTogether" : true,
21+
"rules" : {
22+
"AllPublicDeclarationsHaveDocumentation" : false,
23+
"AlwaysUseLiteralForEmptyCollectionInit" : false,
24+
"AlwaysUseLowerCamelCase" : false,
25+
"AmbiguousTrailingClosureOverload" : true,
26+
"BeginDocumentationCommentWithOneLineSummary" : false,
27+
"DoNotUseSemicolons" : true,
28+
"DontRepeatTypeInStaticProperties" : true,
29+
"FileScopedDeclarationPrivacy" : true,
30+
"FullyIndirectEnum" : true,
31+
"GroupNumericLiterals" : true,
32+
"IdentifiersMustBeASCII" : true,
33+
"NeverForceUnwrap" : false,
34+
"NeverUseForceTry" : false,
35+
"NeverUseImplicitlyUnwrappedOptionals" : false,
36+
"NoAccessLevelOnExtensionDeclaration" : true,
37+
"NoAssignmentInExpressions" : true,
38+
"NoBlockComments" : true,
39+
"NoCasesWithOnlyFallthrough" : true,
40+
"NoEmptyTrailingClosureParentheses" : true,
41+
"NoLabelsInCasePatterns" : true,
42+
"NoLeadingUnderscores" : false,
43+
"NoParensAroundConditions" : true,
44+
"NoVoidReturnOnFunctionSignature" : true,
45+
"OmitExplicitReturns" : true,
46+
"OneCasePerLine" : true,
47+
"OneVariableDeclarationPerLine" : true,
48+
"OnlyOneTrailingClosureArgument" : true,
49+
"OrderedImports" : true,
50+
"ReplaceForEachWithForLoop" : true,
51+
"ReturnVoidInsteadOfEmptyTuple" : true,
52+
"UseEarlyExits" : false,
53+
"UseExplicitNilCheckInConditions" : false,
54+
"UseLetInEveryBoundCaseVariable" : false,
55+
"UseShorthandTypeNames" : true,
56+
"UseSingleLinePropertyGetter" : false,
57+
"UseSynthesizedInitializer" : false,
58+
"UseTripleSlashForDocumentationComments" : true,
59+
"UseWhereClausesInForLoops" : false,
60+
"ValidateDocumentationComments" : false
61+
}
62+
}

.swiftformat

-19
This file was deleted.

Examples/APIGateway/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
samconfig.toml
2+
Makefile

Examples/APIGateway/Package.swift

+55
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

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# API Gateway
2+
3+
This is a simple example of an AWS Lambda function invoked through an Amazon API Gateway.
4+
5+
## Code
6+
7+
The Lambda function takes all HTTP headers it receives as input and returns them as output.
8+
9+
The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when the API Gateway receives an HTTP request.
10+
11+
The handler is `(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response`. The function takes two arguments:
12+
- the event argument is a `APIGatewayV2Request`. It is the parameter passed by the API Gateway. It contains all data passed in the HTTP request and some meta data.
13+
- the context argument is a `Lambda Context`. It is a description of the runtime context.
14+
15+
The function must return a `APIGatewayV2Response`.
16+
17+
`APIGatewayV2Request` and `APIGatewayV2Response` are defined in the [Swift AWS Lambda Events](https://github.com/swift-server/swift-aws-lambda-events) library.
18+
19+
## Build & Package
20+
21+
To build the package, type the following commands.
22+
23+
```bash
24+
swift build
25+
swift package archive --allow-network-access docker
26+
```
27+
28+
If there is no error, there is a ZIP file ready to deploy.
29+
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip`
30+
31+
## Deploy
32+
33+
The deployment must include the Lambda function and the API Gateway. We use the [Serverless Application Model (SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) to deploy the infrastructure.
34+
35+
**Prerequisites** : Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
36+
37+
The example directory contains a file named `template.yaml` that describes the deployment.
38+
39+
To actually deploy your Lambda function and create the infrastructure, type the following `sam` command.
40+
41+
```bash
42+
sam deploy \
43+
----resolve-s3 \
44+
--template-file template.yaml \
45+
--stack-name MyLambda \
46+
--capabilities CAPABILITY_IAM
47+
```
48+
49+
At the end of the deployment, the script lists the API Gateway endpoint.
50+
The output is similar to this one.
51+
52+
```
53+
-----------------------------------------------------------------------------------------------------------------------------
54+
Outputs
55+
-----------------------------------------------------------------------------------------------------------------------------
56+
Key APIGAtewayEndpoint
57+
Description API Gateway endpoint UR"
58+
Value https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
59+
-----------------------------------------------------------------------------------------------------------------------------
60+
```
61+
62+
## Invoke your Lambda function
63+
64+
To invoke the Lambda function, use this `curl` command line.
65+
66+
```bash
67+
curl https://a5q74es3k2.execute-api.us-east-1.amazonaws.com
68+
```
69+
70+
Be sure to replace the URL with the API Gateway endpoint returned in the previous step.
71+
72+
This should print a JSON similar to
73+
74+
```bash
75+
{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"}
76+
```
77+
78+
If you have `jq` installed, you can use it to pretty print the output.
79+
80+
```bash
81+
curl -s https://a5q74es3k2.execute-api.us-east-1.amazonaws.com | jq
82+
{
83+
"version": "2.0",
84+
"rawPath": "/",
85+
"requestContext": {
86+
"domainPrefix": "a5q74es3k2",
87+
"stage": "$default",
88+
"timeEpoch": 1727726558220,
89+
"http": {
90+
"protocol": "HTTP/1.1",
91+
"method": "GET",
92+
"userAgent": "curl/8.7.1",
93+
"path": "/",
94+
"sourceIp": "81.0.0.43"
95+
},
96+
"apiId": "a5q74es3k2",
97+
"accountId": "012345678901",
98+
"requestId": "e72KxgsRoAMEMSA=",
99+
"domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
100+
"time": "30/Sep/2024:20:02:38 +0000"
101+
},
102+
"rawQueryString": "",
103+
"routeKey": "$default",
104+
"headers": {
105+
"x-forwarded-for": "81.0.0.43",
106+
"user-agent": "curl/8.7.1",
107+
"host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
108+
"accept": "*/*",
109+
"x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb",
110+
"content-length": "0",
111+
"x-forwarded-proto": "https",
112+
"x-forwarded-port": "443"
113+
},
114+
"isBase64Encoded": false
115+
}
116+
```
117+
118+
## Undeploy
119+
120+
When done testing, you can delete the infrastructure with this command.
121+
122+
```bash
123+
sam delete
124+
```
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
18+
#if canImport(FoundationEssentials)
19+
import FoundationEssentials
20+
#else
21+
import Foundation
22+
#endif
23+
24+
let encoder = JSONEncoder()
25+
let runtime = LambdaRuntime {
26+
(event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in
27+
28+
var header = HTTPHeaders()
29+
context.logger.debug("HTTP API Message received")
30+
31+
header["content-type"] = "application/json"
32+
33+
// echo the request in the response
34+
let data = try encoder.encode(event)
35+
let response = String(decoding: data, as: Unicode.UTF8.self)
36+
37+
return APIGatewayV2Response(statusCode: .ok, headers: header, body: response)
38+
}
39+
40+
try await runtime.run()

0 commit comments

Comments
 (0)