mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
25eb6e16de
We want that the runtime only depends on `FoundationEssentials` where available (ie. on linux) to ensure small binary size. ### Motivation: Smaller binary size is good for lambda deployment and cold-start times. The runtime should only depend on `FoundationEssentials`. ### Modifications: - replace `import Foundation` with `import FoundationEssentials` if `FoundationEssentials` is available. - I also applied the same treatment to tests to ensure that catch error where tests run on linux and we use API that is only available in `Foundation` which easily happens when you develop on macOS (where always full `Foundation` is available). ### Result: This should allow builds without linking full `Foundation`.
58 lines
1.9 KiB
Swift
58 lines
1.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftAWSLambdaRuntime open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors
|
|
// 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 AWSLambdaRuntime
|
|
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
struct BackgroundProcessingHandler: LambdaWithBackgroundProcessingHandler {
|
|
struct Input: Decodable {
|
|
let message: String
|
|
}
|
|
|
|
struct Greeting: Encodable {
|
|
let echoedMessage: String
|
|
}
|
|
|
|
typealias Event = Input
|
|
typealias Output = Greeting
|
|
|
|
func handle(
|
|
_ event: Event,
|
|
outputWriter: some LambdaResponseWriter<Output>,
|
|
context: LambdaContext
|
|
) async throws {
|
|
// Return result to the Lambda control plane
|
|
context.logger.debug("BackgroundProcessingHandler - message received")
|
|
try await outputWriter.write(Greeting(echoedMessage: event.message))
|
|
|
|
// Perform some background work, e.g:
|
|
context.logger.debug("BackgroundProcessingHandler - response sent. Performing background tasks.")
|
|
try await Task.sleep(for: .seconds(10))
|
|
|
|
// Exit the function. All asynchronous work has been executed before exiting the scope of this function.
|
|
// Follows structured concurrency principles.
|
|
context.logger.debug("BackgroundProcessingHandler - Background tasks completed. Returning")
|
|
return
|
|
}
|
|
}
|
|
|
|
let adapter = LambdaCodableAdapter(handler: BackgroundProcessingHandler())
|
|
let runtime = LambdaRuntime.init(handler: adapter)
|
|
try await runtime.run()
|