Skip to content

Commit fc2e6d5

Browse files
committed
Simplify usage by providing a run() function
1 parent 35d5de6 commit fc2e6d5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/OpenAPILambda.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//===----------------------------------------------------------------------===//
1515
import Foundation
1616
@_exported import AWSLambdaRuntime // re-export AWSLambdaRuntime so that users don't have to.
17+
import Logging
1718
import OpenAPIRuntime
1819
import HTTPTypes
1920

@@ -42,7 +43,28 @@ public protocol OpenAPILambda: Sendable {
4243
extension OpenAPILambda {
4344
/// Returns the Lambda handler function for this OpenAPI Lambda implementation.
4445
/// - Returns: A handler function that can be used with AWS Lambda Runtime
46+
@inlinable
4547
public static func handler() throws -> @Sendable (Event, LambdaContext) async throws -> Output {
4648
try OpenAPILambdaHandler<Self>().handler
4749
}
50+
51+
/// Run the Lambda handler function for this OpenAPI Lambda implementation with a custom logger, when one is given.
52+
/// - Parameter logger: The logger to use for Lambda runtime logging
53+
@inlinable
54+
public static func run(logger: Logger? = nil) async throws {
55+
let lambdaRuntime:
56+
LambdaRuntime<
57+
LambdaCodableAdapter<
58+
LambdaHandlerAdapter<Self.Event, Self.Output, ClosureHandler<Self.Event, Self.Output>>, Self.Event,
59+
Self.Output, LambdaJSONEventDecoder, LambdaJSONOutputEncoder<Self.Output>
60+
>
61+
>
62+
if let logger {
63+
lambdaRuntime = try LambdaRuntime(logger: logger, body: Self.handler())
64+
}
65+
else {
66+
lambdaRuntime = try LambdaRuntime(body: Self.handler())
67+
}
68+
try await lambdaRuntime.run()
69+
}
4870
}

0 commit comments

Comments
 (0)