Skip to content

Commit

Permalink
Add services parameter to Application.init (#438)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adam-fowler authored May 6, 2024
1 parent 877cb49 commit a778e1d
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,15 @@ public struct Application<Responder: HTTPResponder>: 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
public init(
responder: Responder,
server: HTTPChannelBuilder = .http1(),
configuration: ApplicationConfiguration = ApplicationConfiguration(),
services: [Service] = [],
onServerRunning: @escaping @Sendable (Channel) async -> Void = { _ in },
eventLoopGroupProvider: EventLoopGroupProvider = .singleton,
logger: Logger? = nil
Expand All @@ -211,7 +213,7 @@ public struct Application<Responder: HTTPResponder>: ApplicationProtocol where R
self._onServerRunning = onServerRunning

self.eventLoopGroup = eventLoopGroupProvider.eventLoopGroup
self.services = []
self.services = services
self.processesRunBeforeServerStart = []
}

Expand All @@ -221,32 +223,28 @@ public struct Application<Responder: HTTPResponder>: 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
public init<ResponderBuilder: HTTPResponderBuilder>(
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
Expand Down

0 comments on commit a778e1d

Please sign in to comment.