/
Lambda.swift
86 lines (77 loc) · 3 KB
/
Lambda.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import Hummingbird
import Logging
import NIOCore
import NIOPosix
/// Protocol for Hummingbird Lambdas.
///
/// Defines the `Event` and `Output` types, how you convert from `Event` to ``HummingbirdCore/HBRequest``
/// and ``HummingbirdCore/HBResponse`` to `Output`. Create a type conforming to this protocol and tag it
/// with `@main`.
/// ```swift
/// struct MyLambda: HBLambda {
/// typealias Event = APIGatewayRequest
/// typealias Output = APIGatewayResponse
/// typealias Context = MyLambdaRequestContext // must conform to `HBLambdaRequestContext`
///
/// init(context: LambdaInitializationContext) {}
///
/// /// build responder that will create a response from a request
/// func buildResponder() -> some HBResponder<Context> {
/// let router = HBRouter(context: Context.self)
/// router.get("hello") { _,_ in
/// "Hello"
/// }
/// return router.buildResponder()
/// }
/// }
/// ```
/// - SeeAlso: ``HBAPIGatewayLambda`` and ``HBAPIGatewayV2Lambda`` for specializations of this protocol.
public protocol HBLambda {
/// Event that triggers the lambda
associatedtype Event: Decodable
/// Request context
associatedtype Context: HBLambdaRequestContext<Event> = HBBasicLambdaRequestContext<Event>
/// Output of lambda
associatedtype Output: Encodable
/// HTTP Responder
associatedtype Responder: HBResponder<Context>
func buildResponder() -> Responder
/// Initialize application.
init(context: LambdaInitializationContext) async throws
/// Called when Lambda is terminating. This is where you can cleanup any resources
func shutdown() async throws
/// Convert from `In` type to `HBRequest`
/// - Parameters:
/// - context: Lambda context
/// - from: input type
func request(context: LambdaContext, from: Event) throws -> HBRequest
/// Convert from `HBResponse` to `Out` type
/// - Parameter from: response from Hummingbird
func output(from: HBResponse) async throws -> Output
}
extension HBLambda {
/// Initializes and runs the Lambda function.
///
/// If you precede your `EventLoopLambdaHandler` conformer's declaration with the
/// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
/// attribute, the system calls the conformer's `main()` method to launch the lambda function.
public static func main() throws {
HBLambdaHandler<Self>.main()
}
public func shutdown() async throws {}
}