From a778e1d88527b657b47f8b90e39256b1072903bc Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Mon, 6 May 2024 08:04:42 +0100 Subject: [PATCH] Add services parameter to Application.init (#438) * Add services parameter to Application.init Also have router version call responder version of init, instead of copying the code * Don't used shared in Application, as tests fail --- Sources/Hummingbird/Application.swift | 30 +++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Sources/Hummingbird/Application.swift b/Sources/Hummingbird/Application.swift index 272619f72..46ca6b67d 100644 --- a/Sources/Hummingbird/Application.swift +++ b/Sources/Hummingbird/Application.swift @@ -187,6 +187,7 @@ public struct Application: ApplicationProtocol where R /// - responder: HTTP responder. Returns a response based off a request and context /// - server: Server child channel setup (http1, http2, http1WithWebSocketUpgrade etc) /// - configuration: Application configuration + /// - services: List of Services for Application to add to its internal ServiceGroup /// - onServerRunning: Function called once the server is running /// - eventLoopGroupProvider: Where to get our EventLoopGroup /// - logger: Logger application uses @@ -194,6 +195,7 @@ public struct Application: ApplicationProtocol where R responder: Responder, server: HTTPChannelBuilder = .http1(), configuration: ApplicationConfiguration = ApplicationConfiguration(), + services: [Service] = [], onServerRunning: @escaping @Sendable (Channel) async -> Void = { _ in }, eventLoopGroupProvider: EventLoopGroupProvider = .singleton, logger: Logger? = nil @@ -211,7 +213,7 @@ public struct Application: ApplicationProtocol where R self._onServerRunning = onServerRunning self.eventLoopGroup = eventLoopGroupProvider.eventLoopGroup - self.services = [] + self.services = services self.processesRunBeforeServerStart = [] } @@ -221,6 +223,7 @@ public struct Application: ApplicationProtocol where R /// - router: Router used to generate responses from requests /// - server: Server child channel setup (http1, http2, http1WithWebSocketUpgrade etc) /// - configuration: Application configuration + /// - services: List of Services for Application to add to its internal ServiceGroup /// - onServerRunning: Function called once the server is running /// - eventLoopGroupProvider: Where to get our EventLoopGroup /// - logger: Logger application uses @@ -228,25 +231,20 @@ public struct Application: ApplicationProtocol where R router: ResponderBuilder, server: HTTPChannelBuilder = .http1(), configuration: ApplicationConfiguration = ApplicationConfiguration(), + services: [Service] = [], onServerRunning: @escaping @Sendable (Channel) async -> Void = { _ in }, eventLoopGroupProvider: EventLoopGroupProvider = .singleton, logger: Logger? = nil ) where Responder == ResponderBuilder.Responder { - if let logger { - self.logger = logger - } else { - var logger = Logger(label: configuration.serverName ?? "Hummingbird") - logger.logLevel = Environment().get("LOG_LEVEL").map { Logger.Level(rawValue: $0) ?? .info } ?? .info - self.logger = logger - } - self.responder = router.buildResponder() - self.server = server - self.configuration = configuration - self._onServerRunning = onServerRunning - - self.eventLoopGroup = eventLoopGroupProvider.eventLoopGroup - self.services = [] - self.processesRunBeforeServerStart = [] + self.init( + responder: router.buildResponder(), + server: server, + configuration: configuration, + services: services, + onServerRunning: onServerRunning, + eventLoopGroupProvider: eventLoopGroupProvider, + logger: logger + ) } // MARK: Methods