From 1ae5bb5b2a9dacf339d543600fee4116b4135a51 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 16 Dec 2023 16:14:36 +0000 Subject: [PATCH] Rename HBChannelSetup, HBHTTPChannelSetupBuilder --- Sources/Hummingbird/Application.swift | 14 +++++++------- .../{ChannelSetup.swift => ChildChannel.swift} | 4 ++-- .../HummingbirdCore/Server/HTTP/HTTP1Channel.swift | 4 ++-- ...SetupBuilder.swift => HTTPChannelBuilder.swift} | 10 +++++----- .../Server/HTTP/HTTPChannelHandler.swift | 2 +- Sources/HummingbirdCore/Server/Server.swift | 14 +++++++------- Sources/HummingbirdHTTP2/HTTP2Channel.swift | 2 +- ...etupBuilder.swift => HTTP2ChannelBuilder.swift} | 4 ++-- .../{TLSChannelSetup.swift => TLSChannel.swift} | 6 +++--- ...lSetupBuilder.swift => TLSChannelBuilder.swift} | 8 ++++---- Sources/HummingbirdXCT/HBXCTLive.swift | 4 ++-- Tests/HummingbirdCoreTests/TestUtils.swift | 14 +++++++------- 12 files changed, 43 insertions(+), 43 deletions(-) rename Sources/HummingbirdCore/Server/{ChannelSetup.swift => ChildChannel.swift} (84%) rename Sources/HummingbirdCore/Server/HTTP/{HTTPChannelSetupBuilder.swift => HTTPChannelBuilder.swift} (80%) rename Sources/HummingbirdHTTP2/{HTTP2ChannelSetupBuilder.swift => HTTP2ChannelBuilder.swift} (91%) rename Sources/HummingbirdTLS/{TLSChannelSetup.swift => TLSChannel.swift} (77%) rename Sources/HummingbirdTLS/{TLSChannelSetupBuilder.swift => TLSChannelBuilder.swift} (75%) diff --git a/Sources/Hummingbird/Application.swift b/Sources/Hummingbird/Application.swift index 6770f361b..d4da583fd 100644 --- a/Sources/Hummingbird/Application.swift +++ b/Sources/Hummingbird/Application.swift @@ -46,14 +46,14 @@ public protocol HBApplicationProtocol: Service where Context: HBRequestContext { /// Responder that generates a response from a requests and context associatedtype Responder: HBResponder /// Child Channel setup. This defaults to support HTTP1 - associatedtype ChannelSetup: HBChannelSetup & HTTPChannelHandler = HTTP1Channel + associatedtype ChildChannel: HBChildChannel & HTTPChannelHandler = HTTP1Channel /// Context passed with HBRequest to responder typealias Context = Responder.Context /// Build the responder var responder: Responder { get async throws } /// Server channel setup - var channelSetup: HBHTTPChannelSetupBuilder { get } + var channelSetup: HBHTTPChannelBuilder { get } /// event loop group used by application var eventLoopGroup: EventLoopGroup { get } @@ -69,7 +69,7 @@ public protocol HBApplicationProtocol: Service where Context: HBRequestContext { extension HBApplicationProtocol { /// Server channel setup - public var channelSetup: HBHTTPChannelSetupBuilder { .http1() } + public var channelSetup: HBHTTPChannelBuilder { .http1() } } extension HBApplicationProtocol { @@ -161,9 +161,9 @@ public func loggerWithRequestId(_ logger: Logger) -> Logger { /// try await app.runService() /// ``` /// Editing the application setup after calling `runService` will produce undefined behaviour. -public struct HBApplication: HBApplicationProtocol where Responder.Context: HBRequestContext { +public struct HBApplication: HBApplicationProtocol where Responder.Context: HBRequestContext { public typealias Context = Responder.Context - public typealias ChannelSetup = ChannelSetup + public typealias ChildChannel = ChildChannel public typealias Responder = Responder // MARK: Member variables @@ -179,7 +179,7 @@ public struct HBApplication Void /// Server channel setup - public let channelSetup: HBHTTPChannelSetupBuilder + public let channelSetup: HBHTTPChannelBuilder /// services attached to the application. public var services: [any Service] @@ -188,7 +188,7 @@ public struct HBApplication = .http1(), + channelSetup: HBHTTPChannelBuilder = .http1(), configuration: HBApplicationConfiguration = HBApplicationConfiguration(), eventLoopGroupProvider: EventLoopGroupProvider = .singleton ) { diff --git a/Sources/HummingbirdCore/Server/ChannelSetup.swift b/Sources/HummingbirdCore/Server/ChildChannel.swift similarity index 84% rename from Sources/HummingbirdCore/Server/ChannelSetup.swift rename to Sources/HummingbirdCore/Server/ChildChannel.swift index d56faea6c..f492e4372 100644 --- a/Sources/HummingbirdCore/Server/ChannelSetup.swift +++ b/Sources/HummingbirdCore/Server/ChildChannel.swift @@ -16,7 +16,7 @@ import Logging import NIOCore /// HTTPServer child channel setup protocol -public protocol HBChannelSetup: Sendable { +public protocol HBChildChannel: Sendable { associatedtype Value: Sendable /// Initialize channel @@ -24,7 +24,7 @@ public protocol HBChannelSetup: Sendable { /// - channel: channel /// - childHandlers: Channel handlers to add /// - configuration: server configuration - func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture + func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture /// handle async channel func handle(value: Value, logger: Logger) async diff --git a/Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift b/Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift index 775af9eb8..08126148a 100644 --- a/Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift +++ b/Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift @@ -18,7 +18,7 @@ import NIOCore import NIOHTTPTypes import NIOHTTPTypesHTTP1 -public struct HTTP1Channel: HBChannelSetup, HTTPChannelHandler { +public struct HTTP1Channel: HBChildChannel, HTTPChannelHandler { public typealias Value = NIOAsyncChannel public init( @@ -29,7 +29,7 @@ public struct HTTP1Channel: HBChannelSetup, HTTPChannelHandler { self.responder = responder } - public func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { + public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { let childChannelHandlers: [any ChannelHandler] = [HTTP1ToHTTPServerCodec(secure: false)] + self.additionalChannelHandlers() + diff --git a/Sources/HummingbirdCore/Server/HTTP/HTTPChannelSetupBuilder.swift b/Sources/HummingbirdCore/Server/HTTP/HTTPChannelBuilder.swift similarity index 80% rename from Sources/HummingbirdCore/Server/HTTP/HTTPChannelSetupBuilder.swift rename to Sources/HummingbirdCore/Server/HTTP/HTTPChannelBuilder.swift index fc094550b..9aebf62d6 100644 --- a/Sources/HummingbirdCore/Server/HTTP/HTTPChannelSetupBuilder.swift +++ b/Sources/HummingbirdCore/Server/HTTP/HTTPChannelBuilder.swift @@ -15,17 +15,17 @@ import NIOCore /// Build Channel Setup that takes an HTTP responder -public struct HBHTTPChannelSetupBuilder: Sendable { - public let build: @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChannelSetup - public init(_ build: @escaping @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChannelSetup) { +public struct HBHTTPChannelBuilder: Sendable { + public let build: @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChildChannel + public init(_ build: @escaping @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChildChannel) { self.build = build } } -extension HBHTTPChannelSetupBuilder { +extension HBHTTPChannelBuilder { public static func http1( additionalChannelHandlers: @autoclosure @escaping @Sendable () -> [any RemovableChannelHandler] = [] - ) -> HBHTTPChannelSetupBuilder { + ) -> HBHTTPChannelBuilder { return .init { responder in return HTTP1Channel(responder: responder, additionalChannelHandlers: additionalChannelHandlers) } diff --git a/Sources/HummingbirdCore/Server/HTTP/HTTPChannelHandler.swift b/Sources/HummingbirdCore/Server/HTTP/HTTPChannelHandler.swift index 80411dfb0..cef26b7b8 100644 --- a/Sources/HummingbirdCore/Server/HTTP/HTTPChannelHandler.swift +++ b/Sources/HummingbirdCore/Server/HTTP/HTTPChannelHandler.swift @@ -20,7 +20,7 @@ import NIOHTTPTypes import ServiceLifecycle /// Protocol for HTTP channels -public protocol HTTPChannelHandler: HBChannelSetup { +public protocol HTTPChannelHandler: HBChildChannel { typealias Responder = @Sendable (HBRequest, Channel) async throws -> HBResponse var responder: Responder { get } } diff --git a/Sources/HummingbirdCore/Server/Server.swift b/Sources/HummingbirdCore/Server/Server.swift index ff8276824..78585161b 100644 --- a/Sources/HummingbirdCore/Server/Server.swift +++ b/Sources/HummingbirdCore/Server/Server.swift @@ -23,12 +23,12 @@ import NIOTransportServices import ServiceLifecycle /// HTTP server class -public actor HBServer: Service { - public typealias AsyncChildChannel = ChannelSetup.Value +public actor HBServer: Service { + public typealias AsyncChildChannel = ChildChannel.Value public typealias AsyncServerChannel = NIOAsyncChannel enum State: CustomStringConvertible { case initial( - childChannelSetup: ChannelSetup, + childChannelSetup: ChildChannel, configuration: HBServerConfiguration, onServerRunning: (@Sendable (Channel) async -> Void)? ) @@ -72,7 +72,7 @@ public actor HBServer: Service { /// - group: EventLoopGroup server uses /// - configuration: Configuration for server public init( - childChannelSetup: ChannelSetup, + childChannelSetup: ChildChannel, configuration: HBServerConfiguration, onServerRunning: (@Sendable (Channel) async -> Void)? = { _ in }, eventLoopGroup: EventLoopGroup, @@ -175,7 +175,7 @@ public actor HBServer: Service { /// Start server /// - Parameter responder: Object that provides responses to requests sent to the server /// - Returns: EventLoopFuture that is fulfilled when server has started - public func makeServer(childChannelSetup: ChannelSetup, configuration: HBServerConfiguration) async throws -> AsyncServerChannel { + public func makeServer(childChannelSetup: ChildChannel, configuration: HBServerConfiguration) async throws -> AsyncServerChannel { let bootstrap: ServerBootstrapProtocol #if canImport(Network) if let tsBootstrap = self.createTSBootstrap(configuration: configuration) { @@ -204,7 +204,7 @@ public actor HBServer: Service { port: port, serverBackPressureStrategy: nil ) { channel in - childChannelSetup.initialize( + childChannelSetup.setup( channel: channel, configuration: configuration, logger: self.logger @@ -217,7 +217,7 @@ public actor HBServer: Service { cleanupExistingSocketFile: false, serverBackPressureStrategy: nil ) { channel in - childChannelSetup.initialize( + childChannelSetup.setup( channel: channel, configuration: configuration, logger: self.logger diff --git a/Sources/HummingbirdHTTP2/HTTP2Channel.swift b/Sources/HummingbirdHTTP2/HTTP2Channel.swift index 2101f032f..c29cd8b23 100644 --- a/Sources/HummingbirdHTTP2/HTTP2Channel.swift +++ b/Sources/HummingbirdHTTP2/HTTP2Channel.swift @@ -43,7 +43,7 @@ public struct HTTP2Channel: HTTPChannelHandler { self.http1 = HTTP1Channel(responder: responder, additionalChannelHandlers: additionalChannelHandlers) } - public func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { + public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { do { try channel.pipeline.syncOperations.addHandler(NIOSSLServerHandler(context: self.sslContext)) } catch { diff --git a/Sources/HummingbirdHTTP2/HTTP2ChannelSetupBuilder.swift b/Sources/HummingbirdHTTP2/HTTP2ChannelBuilder.swift similarity index 91% rename from Sources/HummingbirdHTTP2/HTTP2ChannelSetupBuilder.swift rename to Sources/HummingbirdHTTP2/HTTP2ChannelBuilder.swift index 01563585e..ddf88f91e 100644 --- a/Sources/HummingbirdHTTP2/HTTP2ChannelSetupBuilder.swift +++ b/Sources/HummingbirdHTTP2/HTTP2ChannelBuilder.swift @@ -16,11 +16,11 @@ import HummingbirdCore import NIOCore import NIOSSL -extension HBHTTPChannelSetupBuilder { +extension HBHTTPChannelBuilder { public static func http2( tlsConfiguration: TLSConfiguration, additionalChannelHandlers: @autoclosure @escaping @Sendable () -> [any RemovableChannelHandler] = [] - ) throws -> HBHTTPChannelSetupBuilder { + ) throws -> HBHTTPChannelBuilder { return .init { responder in return try HTTP2Channel( tlsConfiguration: tlsConfiguration, diff --git a/Sources/HummingbirdTLS/TLSChannelSetup.swift b/Sources/HummingbirdTLS/TLSChannel.swift similarity index 77% rename from Sources/HummingbirdTLS/TLSChannelSetup.swift rename to Sources/HummingbirdTLS/TLSChannel.swift index 90eb03a9e..cbc2d8d3d 100644 --- a/Sources/HummingbirdTLS/TLSChannelSetup.swift +++ b/Sources/HummingbirdTLS/TLSChannel.swift @@ -4,7 +4,7 @@ import NIOCore import NIOSSL /// Sets up child channel to use TLS before accessing base channel setup -public struct TLSChannel: HBChannelSetup { +public struct TLSChannel: HBChildChannel { public typealias Value = BaseChannel.Value public init(_ baseChannel: BaseChannel, tlsConfiguration: TLSConfiguration) throws { @@ -13,9 +13,9 @@ public struct TLSChannel: HBChannelSetup { } @inlinable - public func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { + public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture { return channel.pipeline.addHandler(NIOSSLServerHandler(context: self.sslContext)).flatMap { - self.baseChannel.initialize(channel: channel, configuration: configuration, logger: logger) + self.baseChannel.setup(channel: channel, configuration: configuration, logger: logger) } } diff --git a/Sources/HummingbirdTLS/TLSChannelSetupBuilder.swift b/Sources/HummingbirdTLS/TLSChannelBuilder.swift similarity index 75% rename from Sources/HummingbirdTLS/TLSChannelSetupBuilder.swift rename to Sources/HummingbirdTLS/TLSChannelBuilder.swift index 3035db9ca..1d217acc3 100644 --- a/Sources/HummingbirdTLS/TLSChannelSetupBuilder.swift +++ b/Sources/HummingbirdTLS/TLSChannelBuilder.swift @@ -15,11 +15,11 @@ import HummingbirdCore import NIOSSL -extension HBHTTPChannelSetupBuilder { - public static func tls( - _ base: HBHTTPChannelSetupBuilder = .http1(), +extension HBHTTPChannelBuilder { + public static func tls( + _ base: HBHTTPChannelBuilder = .http1(), tlsConfiguration: TLSConfiguration - ) throws -> HBHTTPChannelSetupBuilder> { + ) throws -> HBHTTPChannelBuilder> { return .init { responder in return try TLSChannel(base.build(responder), tlsConfiguration: tlsConfiguration) } diff --git a/Sources/HummingbirdXCT/HBXCTLive.swift b/Sources/HummingbirdXCT/HBXCTLive.swift index d2a5cd4da..f2053feb5 100644 --- a/Sources/HummingbirdXCT/HBXCTLive.swift +++ b/Sources/HummingbirdXCT/HBXCTLive.swift @@ -28,7 +28,7 @@ final class HBXCTLive: HBXCTApplication { /// TestApplication used to wrap HBApplication being tested struct TestApplication: HBApplicationProtocol, Service { typealias Responder = BaseApp.Responder - typealias ChannelSetup = BaseApp.ChannelSetup + typealias ChildChannel = BaseApp.ChildChannel let base: BaseApp @@ -36,7 +36,7 @@ final class HBXCTLive: HBXCTApplication { get async throws { try await self.base.responder } } - var channelSetup: HBHTTPChannelSetupBuilder { + var channelSetup: HBHTTPChannelBuilder { self.base.channelSetup } diff --git a/Tests/HummingbirdCoreTests/TestUtils.swift b/Tests/HummingbirdCoreTests/TestUtils.swift index 59be1ff58..20380bd75 100644 --- a/Tests/HummingbirdCoreTests/TestUtils.swift +++ b/Tests/HummingbirdCoreTests/TestUtils.swift @@ -31,13 +31,13 @@ public enum TestErrors: Error { } /// Helper function for testing a server -public func testServer( +public func testServer( responder: @escaping HTTPChannelHandler.Responder, - httpChannelSetup: HBHTTPChannelSetupBuilder, + httpChannelSetup: HBHTTPChannelBuilder, configuration: HBServerConfiguration, eventLoopGroup: EventLoopGroup, logger: Logger, - _ test: @escaping @Sendable (HBServer, Int) async throws -> Value + _ test: @escaping @Sendable (HBServer, Int) async throws -> Value ) async throws -> Value { try await withThrowingTaskGroup(of: Void.self) { group in let promise = Promise() @@ -68,14 +68,14 @@ public func testServer( /// /// Creates test client, runs test function abd ensures everything is /// shutdown correctly -public func testServer( +public func testServer( responder: @escaping HTTPChannelHandler.Responder, - httpChannelSetup: HBHTTPChannelSetupBuilder, + httpChannelSetup: HBHTTPChannelBuilder, configuration: HBServerConfiguration, eventLoopGroup: EventLoopGroup, logger: Logger, clientConfiguration: HBXCTClient.Configuration = .init(), - _ test: @escaping @Sendable (HBServer, HBXCTClient) async throws -> Value + _ test: @escaping @Sendable (HBServer, HBXCTClient) async throws -> Value ) async throws -> Value { try await withThrowingTaskGroup(of: Void.self) { group in let promise = Promise() @@ -112,7 +112,7 @@ public func testServer( public func testServer( responder: @escaping HTTPChannelHandler.Responder, - httpChannelSetup: HBHTTPChannelSetupBuilder = .http1(), + httpChannelSetup: HBHTTPChannelBuilder = .http1(), configuration: HBServerConfiguration, eventLoopGroup: EventLoopGroup, logger: Logger,