diff --git a/Examples/ServiceLifecycle+Postgres/Package.swift b/Examples/ServiceLifecycle+Postgres/Package.swift index e06ba8d7..d8b271fd 100644 --- a/Examples/ServiceLifecycle+Postgres/Package.swift +++ b/Examples/ServiceLifecycle+Postgres/Package.swift @@ -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( diff --git a/Examples/ServiceLifecycle+Postgres/Sources/Lambda.swift b/Examples/ServiceLifecycle+Postgres/Sources/Lambda.swift index 18b81bdd..edd1ff99 100644 --- a/Examples/ServiceLifecycle+Postgres/Sources/Lambda.swift +++ b/Examples/ServiceLifecycle+Postgres/Sources/Lambda.swift @@ -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( diff --git a/Sources/AWSLambdaRuntime/ControlPlaneRequest.swift b/Sources/AWSLambdaRuntime/HTTPClient/ControlPlaneRequest.swift similarity index 100% rename from Sources/AWSLambdaRuntime/ControlPlaneRequest.swift rename to Sources/AWSLambdaRuntime/HTTPClient/ControlPlaneRequest.swift diff --git a/Sources/AWSLambdaRuntime/ControlPlaneRequestEncoder.swift b/Sources/AWSLambdaRuntime/HTTPClient/ControlPlaneRequestEncoder.swift similarity index 100% rename from Sources/AWSLambdaRuntime/ControlPlaneRequestEncoder.swift rename to Sources/AWSLambdaRuntime/HTTPClient/ControlPlaneRequestEncoder.swift diff --git a/Sources/AWSLambdaRuntime/LambdaRuntimeClient+ChannelHandler.swift b/Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClient+ChannelHandler.swift similarity index 100% rename from Sources/AWSLambdaRuntime/LambdaRuntimeClient+ChannelHandler.swift rename to Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClient+ChannelHandler.swift diff --git a/Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift b/Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClient.swift similarity index 100% rename from Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift rename to Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClient.swift diff --git a/Sources/AWSLambdaRuntime/LambdaRuntimeClientProtocol.swift b/Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClientProtocol.swift similarity index 100% rename from Sources/AWSLambdaRuntime/LambdaRuntimeClientProtocol.swift rename to Sources/AWSLambdaRuntime/HTTPClient/LambdaRuntimeClientProtocol.swift diff --git a/Sources/AWSLambdaRuntime/Lambda+LocalServer+Pool.swift b/Sources/AWSLambdaRuntime/HTTPServer/Lambda+LocalServer+Pool.swift similarity index 100% rename from Sources/AWSLambdaRuntime/Lambda+LocalServer+Pool.swift rename to Sources/AWSLambdaRuntime/HTTPServer/Lambda+LocalServer+Pool.swift diff --git a/Sources/AWSLambdaRuntime/Lambda+LocalServer.swift b/Sources/AWSLambdaRuntime/HTTPServer/Lambda+LocalServer.swift similarity index 100% rename from Sources/AWSLambdaRuntime/Lambda+LocalServer.swift rename to Sources/AWSLambdaRuntime/HTTPServer/Lambda+LocalServer.swift diff --git a/Sources/AWSLambdaRuntime/LambdaHandlers.swift b/Sources/AWSLambdaRuntime/Runtime/LambdaHandlers.swift similarity index 75% rename from Sources/AWSLambdaRuntime/LambdaHandlers.swift rename to Sources/AWSLambdaRuntime/Runtime/LambdaHandlers.swift index 9ff33121..8b06b5fb 100644 --- a/Sources/AWSLambdaRuntime/LambdaHandlers.swift +++ b/Sources/AWSLambdaRuntime/Runtime/LambdaHandlers.swift @@ -128,7 +128,7 @@ public protocol LambdaResponseWriter { /// 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. @@ -180,82 +180,3 @@ public struct ClosureHandler: 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, - 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( - decoder: sending Decoder, - logger: Logger = Logger(label: "LambdaRuntime"), - body: sending @escaping (Event, LambdaContext) async throws -> Void - ) - where - Handler == LambdaCodableAdapter< - LambdaHandlerAdapter>, - Event, - Void, - Decoder, - VoidEncoder - > - { - let handler = LambdaCodableAdapter( - decoder: decoder, - handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) - ) - - self.init(handler: handler, logger: logger) - } -} diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Codable.swift similarity index 100% rename from Sources/AWSLambdaRuntime/Lambda+Codable.swift rename to Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Codable.swift diff --git a/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Handler.swift b/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Handler.swift new file mode 100644 index 00000000..9bad332d --- /dev/null +++ b/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Handler.swift @@ -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, + 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( + decoder: sending Decoder, + logger: Logger = Logger(label: "LambdaRuntime"), + body: sending @escaping (Event, LambdaContext) async throws -> Void + ) + where + Handler == LambdaCodableAdapter< + LambdaHandlerAdapter>, + Event, + Void, + Decoder, + VoidEncoder + > + { + let handler = LambdaCodableAdapter( + decoder: decoder, + handler: LambdaHandlerAdapter(handler: ClosureHandler(body: body)) + ) + + self.init(handler: handler, logger: logger) + } +} diff --git a/Sources/AWSLambdaRuntime/LambdaRuntime+ServiceLifecycle.swift b/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+ServiceLifecycle.swift similarity index 100% rename from Sources/AWSLambdaRuntime/LambdaRuntime+ServiceLifecycle.swift rename to Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+ServiceLifecycle.swift diff --git a/Sources/AWSLambdaRuntime/LambdaRuntime.swift b/Sources/AWSLambdaRuntime/Runtime/LambdaRuntime.swift similarity index 100% rename from Sources/AWSLambdaRuntime/LambdaRuntime.swift rename to Sources/AWSLambdaRuntime/Runtime/LambdaRuntime.swift