Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Examples/ServiceLifecycle+Postgres/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let package = Package(
// For standalone usage, comment the line above and uncomment below:
// .package(url: "https://github.com/awslabs/swift-aws-lambda-runtime.git", from: "2.0.0"),

.package(url: "https://github.com/awslabs/swift-aws-lambda-events.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.26.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.3"),
.package(url: "https://github.com/awslabs/swift-aws-lambda-events.git", from: "1.4.0"),
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.30.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.9.1"),
],
targets: [
.executableTarget(
Expand Down
2 changes: 1 addition & 1 deletion Examples/ServiceLifecycle+Postgres/Sources/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct LambdaFunction {

private init() throws {
var logger = Logger(label: "ServiceLifecycleExample")
logger.logLevel = Lambda.env("LOG_LEVEL").flatMap(Logger.Level.init) ?? .info
logger.logLevel = Lambda.env("LOG_LEVEL").flatMap { .init(rawValue: $0) } ?? .info
self.logger = logger

self.pgClient = try LambdaFunction.createPostgresClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public protocol LambdaResponseWriter<Output> {
/// A ``StreamingLambdaHandler`` conforming handler object that can be constructed with a closure.
/// Allows for a handler to be defined in a clean manner, leveraging Swift's trailing closure syntax.
@available(LambdaSwift 2.0, *)
public struct StreamingClosureHandler: StreamingLambdaHandler {
public struct StreamingClosureHandler: StreamingLambdaHandler & Sendable {
let body: @Sendable (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void

/// Initialize an instance from a handler function in the form of a closure.
Expand Down Expand Up @@ -180,82 +180,3 @@ public struct ClosureHandler<Event: Decodable, Output>: LambdaHandler {
try await self.body(event, context)
}
}

@available(LambdaSwift 2.0, *)
extension LambdaRuntime {
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
/// - Parameter
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init(
logger: Logger = Logger(label: "LambdaRuntime"),
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void

) where Handler == StreamingClosureHandler {
self.init(handler: StreamingClosureHandler(body: body), logger: logger)
}

/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
/// - Parameters:
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`.
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init<
Event: Decodable,
Output: Encodable,
Encoder: LambdaOutputEncoder,
Decoder: LambdaEventDecoder
>(
encoder: sending Encoder,
decoder: sending Decoder,
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Output
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
Event,
Output,
Decoder,
Encoder
>
{
let closureHandler = ClosureHandler(body: body)
let streamingAdapter = LambdaHandlerAdapter(handler: closureHandler)
let codableWrapper = LambdaCodableAdapter(
encoder: encoder,
decoder: decoder,
handler: streamingAdapter
)

self.init(handler: codableWrapper, logger: logger)
}

/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
/// - Parameters:
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
decoder: sending Decoder,
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Void
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
Event,
Void,
Decoder,
VoidEncoder
>
{
let handler = LambdaCodableAdapter(
decoder: decoder,
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
)

self.init(handler: handler, logger: logger)
}
}
96 changes: 96 additions & 0 deletions Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Handler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright SwiftAWSLambdaRuntime project authors
// Copyright (c) Amazon.com, Inc. or its affiliates.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Logging
import NIOCore

@available(LambdaSwift 2.0, *)
extension LambdaRuntime {
/// Initialize an instance with a ``StreamingLambdaHandler`` in the form of a closure.
/// - Parameter
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init(
logger: Logger = Logger(label: "LambdaRuntime"),
body: @Sendable @escaping (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> Void

) where Handler == StreamingClosureHandler {
self.init(handler: StreamingClosureHandler(body: body), logger: logger)
}

/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a non-`Void` return type**, an encoder, and a decoder.
/// - Parameters:
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`.
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init<
Event: Decodable,
Output: Encodable,
Encoder: LambdaOutputEncoder,
Decoder: LambdaEventDecoder
>(
encoder: sending Encoder,
decoder: sending Decoder,
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Output
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Output, ClosureHandler<Event, Output>>,
Event,
Output,
Decoder,
Encoder
>
{
let closureHandler = ClosureHandler(body: body)
let streamingAdapter = LambdaHandlerAdapter(handler: closureHandler)
let codableWrapper = LambdaCodableAdapter(
encoder: encoder,
decoder: decoder,
handler: streamingAdapter
)

self.init(handler: codableWrapper, logger: logger)
}

/// Initialize an instance with a ``LambdaHandler`` defined in the form of a closure **with a `Void` return type**, an encoder, and a decoder.
/// - Parameters:
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init<Event: Decodable, Decoder: LambdaEventDecoder>(
decoder: sending Decoder,
logger: Logger = Logger(label: "LambdaRuntime"),
body: sending @escaping (Event, LambdaContext) async throws -> Void
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Void, ClosureHandler<Event, Void>>,
Event,
Void,
Decoder,
VoidEncoder
>
{
let handler = LambdaCodableAdapter(
decoder: decoder,
handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body))
)

self.init(handler: handler, logger: logger)
}
}