Skip to content

Commit db5257c

Browse files
committed
initial v2 commit
1 parent 6a54484 commit db5257c

14 files changed

+122
-149
lines changed

Examples/quoteapi/.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/.aws-sam
2-
/.build
3-
/.swiftpm
4-
/.vscode
1+
.aws-sam
2+
.build
3+
.swiftpm
4+
.vscode
55
Package.resolved
66
samconfig.toml
77
*.d

Examples/quoteapi/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# image used to compile your Swift code
2-
FROM public.ecr.aws/docker/library/swift:5.9.1-amazonlinux2
2+
FROM public.ecr.aws/docker/library/swift:6.1-amazonlinux2
33
RUN yum -y install git jq tar zip openssl-devel

Examples/quoteapi/Makefile

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@ builder-bot:
3030
$(eval $@ARTIFACTS_DIR = $(PWD)/.aws-sam/build/$($@PRODUCT))
3131

3232
## Building from swift-openapi-lambda in a local directory (not from Github)
33-
## 1. git clone https://github.com/swift-server/swift-openapi-lambda ..
34-
## 2. Change `Package.swift` dependency to ../swift-openapi-lambda
33+
## 2. Change `Package.swift` dependency to path: "../.."
3534

36-
## 3. add /.. to BUILD_SRC
37-
## $(eval $@BUILD_SRC = $(PWD)/..)
38-
$(eval $@BUILD_SRC = $(PWD))
35+
## 3. add /../.. to BUILD_SRC
36+
$(eval $@BUILD_SRC = $(PWD)/../..)
37+
## $(eval $@BUILD_SRC = $(PWD))
3938

40-
## 4. add `cd quoteapi &&` to the docker BUILD_CMD
41-
## $(eval $@BUILD_CMD = "ls && cd quoteapi && swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
42-
43-
$(eval $@BUILD_CMD = "swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
39+
## 4. add `cd Examples/quoteapi &&` to the docker BUILD_CMD
40+
$(eval $@BUILD_CMD = "ls && cd Examples/quoteapi && swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
41+
## $(eval $@BUILD_CMD = "swift build --static-swift-stdlib --product $($@PRODUCT) -c release --build-path /build-target")
4442

4543
# build docker image to compile Swift for Linux
4644
docker build -f Dockerfile . -t swift-builder

Examples/quoteapi/Package.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "QuoteService",
88
platforms: [
9-
.macOS(.v13), .iOS(.v15), .tvOS(.v15), .watchOS(.v6),
9+
.macOS(.v15)
1010
],
1111
products: [
1212
.executable(name: "QuoteService", targets: ["QuoteService"])
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.4.0"),
16-
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.5.0"),
17-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
18-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "0.4.0"),
19-
.package(url: "https://github.com/swift-server/swift-openapi-lambda.git", from: "0.2.0"),
20-
// .package(name: "swift-openapi-lambda", path: "../swift-openapi-lambda")
16+
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.8.2"),
17+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta.1"),
18+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
19+
// .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.4.0"),
20+
// .package(url: "https://github.com/swift-server/swift-openapi-lambda.git", from: "0.3.0"),
21+
.package(name: "swift-openapi-lambda", path: "../.."),
2122
],
2223
targets: [
2324
.executableTarget(

Examples/quoteapi/Sources/QuoteService.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//
1414
//===----------------------------------------------------------------------===//
1515

16+
import AWSLambdaRuntime
1617
import Foundation
18+
import Logging
1719
import OpenAPIRuntime
1820
import OpenAPILambda
1921

@@ -24,6 +26,15 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
2426
try self.registerHandlers(on: transport)
2527
}
2628

29+
static func main() async throws {
30+
let logger = Logger(label: "OpenAPILambda")
31+
let lambda = try OpenAPILambdaHandler<QuoteServiceImpl>()
32+
33+
// Instantiate LambdaRuntime with a handler implementing the business logic of the Lambda function
34+
let lambdaRuntime = LambdaRuntime(logger: logger, body: lambda.handler)
35+
try await lambdaRuntime.run()
36+
}
37+
2738
func getQuote(_ input: Operations.getQuote.Input) async throws -> Operations.getQuote.Output {
2839

2940
let symbol = input.path.symbol

Package.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
// swift-tools-version: 6.0
1+
// swift-tools-version: 6.1
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
55

66
let package = Package(
77
name: "swift-openapi-lambda",
8-
platforms: [.macOS(.v12)],
8+
platforms: [.macOS(.v15)],
99
products: [.library(name: "OpenAPILambda", targets: ["OpenAPILambda"])],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"),
12-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
13-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.0.0"),
14-
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
11+
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.8.2"),
12+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta.1"),
13+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
14+
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.4.0"),
1515
],
1616
targets: [
1717
.target(

[email protected]

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

[email protected]

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

Sources/OpenAPILambda.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import OpenAPIRuntime
1818
import HTTPTypes
1919

2020
/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime)
21-
public protocol OpenAPILambda {
21+
public protocol OpenAPILambda: Sendable {
2222

23-
associatedtype Event: Decodable
24-
associatedtype Output: Encodable
23+
associatedtype Event: Decodable, Sendable
24+
associatedtype Output: Encodable, Sendable
2525

2626
/// Initialize application.
2727
///
@@ -38,12 +38,3 @@ public protocol OpenAPILambda {
3838
/// - Parameter from: response from OpenAPIRuntime
3939
func output(from: OpenAPILambdaResponse) -> Output
4040
}
41-
42-
extension OpenAPILambda {
43-
/// Initializes and runs the Lambda function.
44-
///
45-
/// If you precede your ``EventLoopLambdaHandler`` conformer's declaration with the
46-
/// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
47-
/// attribute, the system calls the conformer's `main()` method to launch the lambda function.
48-
public static func main() throws { OpenAPILambdaHandler<Self>.main() }
49-
}

Sources/OpenAPILambdaHandler.swift

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import OpenAPIRuntime
1818
import HTTPTypes
1919

2020
/// Specialization of LambdaHandler which runs an OpenAPILambda
21-
public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
21+
public struct OpenAPILambdaHandler<L: OpenAPILambda>: Sendable where L.Event: Sendable, L.Output: Sendable {
22+
23+
private let router: OpenAPILambdaRouter
24+
private let transport: OpenAPILambdaTransport
25+
private let lambda: L
2226

2327
/// the input type for this Lambda handler (received from the `OpenAPILambda`)
2428
public typealias Event = L.Event
@@ -31,7 +35,7 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
3135
/// Create application, set it up and create `OpenAPILambda` from application and create responder
3236
/// - Parameters
3337
/// - context: Lambda initialization context
34-
public init(context: LambdaInitializationContext) throws {
38+
public init() throws {
3539
self.router = TrieRouter()
3640
self.transport = OpenAPILambdaTransport(router: self.router)
3741
self.lambda = try .init(transport: self.transport)
@@ -45,14 +49,14 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
4549
/// - context: Runtime ``LambdaContext``.
4650
///
4751
/// - Returns: A Lambda result ot type `Output`.
48-
public func handle(_ request: Event, context: LambdaContext) async throws -> Output {
52+
public func handler(event: L.Event, context: LambdaContext) async throws -> L.Output {
4953

50-
// by default return HTTP 500
54+
// by default returns HTTP 500
5155
var lambdaResponse: OpenAPILambdaResponse = (HTTPResponse(status: .internalServerError), "unknown error")
5256

5357
do {
5458
// convert Lambda event source to OpenAPILambdaRequest
55-
let request = try lambda.request(context: context, from: request)
59+
let request = try lambda.request(context: context, from: event)
5660

5761
// route the request to find the handlers and extract the paramaters
5862
let (handler, parameters) = try await router.route(method: request.0.method, path: request.0.path!)
@@ -103,8 +107,28 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
103107
// transform the OpenAPILambdaResponse to the Lambda Output
104108
return lambda.output(from: lambdaResponse)
105109
}
106-
107-
let router: OpenAPILambdaRouter
108-
let transport: OpenAPILambdaTransport
109-
let lambda: L
110110
}
111+
112+
// struct Test<L: OpenAPILambda> where L.Event: Sendable, L.Output: Sendable {
113+
// private let router: OpenAPILambdaRouter
114+
// private let transport: OpenAPILambdaTransport
115+
// private let lambda: L
116+
// private let logger: Logger = Logger(label: "test")
117+
118+
// /// the input type for this Lambda handler (received from the `OpenAPILambda`)
119+
// public typealias Event = L.Event
120+
121+
// /// the output type for this Lambda handler (received from the `OpenAPILambda`)
122+
// public typealias Output = L.Output
123+
124+
// /// Function entry point when the runtime environment is created
125+
// private func main() async throws {
126+
// // Instantiate LambdaRuntime with a handler implementing the business logic of the Lambda function
127+
// let lambdaRuntime = LambdaRuntime(logger: logger, body: self.handler)
128+
// try await lambdaRuntime.run()
129+
// }
130+
// private func handler(event: Event, context: LambdaContext) async throws -> Output {
131+
// fatalError()
132+
// }
133+
134+
// }

0 commit comments

Comments
 (0)