Refactor project directories (#621)

This PR refactors the project's directories.
As the number of source files grows, I created subdirectories to
separate the runtime itself, from its HTTP Client (`RuntimeClient`) and
local HTTP Server (`Lambda+LocalServer`).

The new layout looks like this:

```text
Sources
├── AWSLambdaRuntime
│   ├── FoundationSupport
│   │   ├── Context+Foundation.swift
│   │   ├── Lambda+JSON.swift
│   │   └── Vendored
│   │       ├── ByteBuffer-foundation.swift
│   │       └── JSON+ByteBuffer.swift
│   ├── HTTPClient
│   │   ├── ControlPlaneRequest.swift
│   │   ├── ControlPlaneRequestEncoder.swift
│   │   ├── LambdaRuntimeClient+ChannelHandler.swift
│   │   ├── LambdaRuntimeClient.swift
│   │   └── LambdaRuntimeClientProtocol.swift
│   ├── HTTPServer
│   │   ├── Lambda+LocalServer+Pool.swift
│   │   └── Lambda+LocalServer.swift
│   ├── Lambda.swift
│   ├── LambdaClock.swift
│   ├── LambdaContext.swift
│   ├── LambdaRequestID.swift
│   ├── LambdaResponseStreamWriter+Headers.swift
│   ├── LambdaRuntimeError.swift
│   ├── Runtime
│   │   ├── LambdaHandlers.swift
│   │   ├── LambdaRuntime+Codable.swift
│   │   ├── LambdaRuntime+Handler.swift
│   │   ├── LambdaRuntime+ServiceLifecycle.swift
│   │   └── LambdaRuntime.swift
│   ├── SendableMetatype.swift
│   ├── Utils.swift
│   └── Version.swift
└── MockServer
    └── MockHTTPServer.swift
```
This commit is contained in:
Sébastien Stormacq
2026-01-01 11:19:13 -05:00
committed by GitHub
parent 1f0888ee7b
commit e0f064a93e
14 changed files with 101 additions and 84 deletions
@@ -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(
@@ -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(
@@ -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.
@@ -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)
}
}
@@ -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)
}
}