|
1 |
| -// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless |
| 1 | +// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless |
2 | 2 | //
|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | // you may not use this file except in compliance with the License.
|
|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -import AsyncHTTPClient |
16 | 15 | import AWSLambdaEvents
|
17 | 16 | import AWSLambdaRuntime
|
18 |
| -import AWSLambdaRuntimeCore |
19 |
| -import Foundation |
| 17 | +import ServiceLifecycle |
| 18 | +import Logging |
| 19 | +import NIOCore |
20 | 20 |
|
21 |
| -public extension LambdaInitializationContext { |
22 |
| - enum WebHook { |
23 |
| - public static var timeout: Int64 = 30 |
24 |
| - } |
25 |
| -} |
26 |
| - |
27 |
| -public struct HandlerContext { |
28 |
| - public let handler: String? |
29 |
| - public let httpClient: HTTPClient |
30 |
| -} |
31 |
| - |
32 |
| -public class BreezeLambdaWebHook<Handler: BreezeLambdaWebHookHandler>: LambdaHandler { |
33 |
| - public typealias Event = APIGatewayV2Request |
34 |
| - public typealias Output = APIGatewayV2Response |
| 21 | +/// The Service that handles Breeze Lambda WebHook functionality. |
| 22 | +public struct BreezeLambdaWebHook<LambdaHandler: BreezeLambdaWebHookHandler>: Service { |
35 | 23 |
|
36 |
| - let handlerContext: HandlerContext |
37 |
| - |
38 |
| - public required init(context: LambdaInitializationContext) async throws { |
39 |
| - let handler = Lambda.env("_HANDLER") |
40 |
| - context.logger.info("handler: \(handler ?? "")") |
41 |
| - |
42 |
| - let timeout = HTTPClient.Configuration.Timeout( |
43 |
| - connect: .seconds(LambdaInitializationContext.WebHook.timeout), |
44 |
| - read: .seconds(LambdaInitializationContext.WebHook.timeout) |
45 |
| - ) |
46 |
| - |
47 |
| - let configuration = HTTPClient.Configuration(timeout: timeout) |
48 |
| - let httpClient = HTTPClient( |
49 |
| - eventLoopGroupProvider: .shared(context.eventLoop), |
50 |
| - configuration: configuration |
| 24 | + /// The name of the service, used for logging and identification. |
| 25 | + public let name: String |
| 26 | + /// Configuration for the Breeze HTTP Client. |
| 27 | + public let config: BreezeHTTPClientConfig |
| 28 | + |
| 29 | + /// Initializes a new instance of with the given name and configuration. |
| 30 | + /// - Parameters: |
| 31 | + /// - name: The name of the service. |
| 32 | + /// - config: Configuration for the Breeze HTTP Client. |
| 33 | + /// |
| 34 | + /// This initializer sets up the Breeze Lambda WebHook service with a specified name and configuration. |
| 35 | + /// |
| 36 | + /// - Note: If no configuration is provided, a default configuration with a 30-second timeout and a logger will be used. |
| 37 | + public init( |
| 38 | + name: String, |
| 39 | + config: BreezeHTTPClientConfig? = nil |
| 40 | + ) { |
| 41 | + self.name = name |
| 42 | + let defaultConfig = BreezeHTTPClientConfig( |
| 43 | + timeout: .seconds(30), |
| 44 | + logger: Logger(label: "\(name)") |
51 | 45 | )
|
52 |
| - |
53 |
| - handlerContext = HandlerContext(handler: handler, httpClient: httpClient) |
54 |
| - |
55 |
| - context.terminator.register(name: "shutdown") { eventLoop in |
56 |
| - context.logger.info("shutdown: started") |
57 |
| - let promise = eventLoop.makePromise(of: Void.self) |
58 |
| - Task { |
59 |
| - do { |
60 |
| - try await self.handlerContext.httpClient.shutdown() |
61 |
| - promise.succeed() |
62 |
| - context.logger.info("shutdown: succeed") |
63 |
| - } catch { |
64 |
| - promise.fail(error) |
65 |
| - context.logger.info("shutdown: fail") |
66 |
| - } |
67 |
| - } |
68 |
| - return promise.futureResult |
69 |
| - } |
| 46 | + self.config = config ?? defaultConfig |
70 | 47 | }
|
71 |
| - |
72 |
| - public func handle(_ event: AWSLambdaEvents.APIGatewayV2Request, context: AWSLambdaRuntimeCore.LambdaContext) async throws -> AWSLambdaEvents.APIGatewayV2Response { |
73 |
| - return await Handler(handlerContext: handlerContext).handle(context: context, event: event) |
| 48 | + |
| 49 | + /// Runs the Breeze Lambda WebHook service. |
| 50 | + /// - Throws: An error if the service fails to start or run. |
| 51 | + /// |
| 52 | + /// This method initializes the Breeze Lambda WebHook service and starts it, |
| 53 | + /// handling any errors that may occur during the process. |
| 54 | + /// It gracefully shuts down the service on termination signals. |
| 55 | + public func run() async throws { |
| 56 | + do { |
| 57 | + let lambdaService = BreezeLambdaWebHookService<LambdaHandler>( |
| 58 | + config: config |
| 59 | + ) |
| 60 | + let serviceGroup = ServiceGroup( |
| 61 | + services: [lambdaService], |
| 62 | + gracefulShutdownSignals: [.sigterm, .sigint], |
| 63 | + logger: config.logger |
| 64 | + ) |
| 65 | + config.logger.error("Starting \(name) ...") |
| 66 | + try await serviceGroup.run() |
| 67 | + } catch { |
| 68 | + config.logger.error("Error running \(name): \(error.localizedDescription)") |
| 69 | + } |
74 | 70 | }
|
75 | 71 | }
|
76 |
| - |
0 commit comments