Skip to content

Commit

Permalink
Rename HBChannelSetup, HBHTTPChannelSetupBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 16, 2023
1 parent 70e5372 commit 1ae5bb5
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
14 changes: 7 additions & 7 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChannelSetup> { get }
var channelSetup: HBHTTPChannelBuilder<ChildChannel> { get }

/// event loop group used by application
var eventLoopGroup: EventLoopGroup { get }
Expand All @@ -69,7 +69,7 @@ public protocol HBApplicationProtocol: Service where Context: HBRequestContext {

extension HBApplicationProtocol {
/// Server channel setup
public var channelSetup: HBHTTPChannelSetupBuilder<HTTP1Channel> { .http1() }
public var channelSetup: HBHTTPChannelBuilder<HTTP1Channel> { .http1() }
}

extension HBApplicationProtocol {
Expand Down Expand Up @@ -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<Responder: HBResponder, ChannelSetup: HBChannelSetup & HTTPChannelHandler>: HBApplicationProtocol where Responder.Context: HBRequestContext {
public struct HBApplication<Responder: HBResponder, ChildChannel: HBChildChannel & HTTPChannelHandler>: 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
Expand All @@ -179,7 +179,7 @@ public struct HBApplication<Responder: HBResponder, ChannelSetup: HBChannelSetup
/// on server running
private var _onServerRunning: @Sendable (Channel) async -> Void
/// Server channel setup
public let channelSetup: HBHTTPChannelSetupBuilder<ChannelSetup>
public let channelSetup: HBHTTPChannelBuilder<ChildChannel>
/// services attached to the application.
public var services: [any Service]

Expand All @@ -188,7 +188,7 @@ public struct HBApplication<Responder: HBResponder, ChannelSetup: HBChannelSetup
/// Initialize new Application
public init(
responder: Responder,
channelSetup: HBHTTPChannelSetupBuilder<ChannelSetup> = .http1(),
channelSetup: HBHTTPChannelBuilder<ChildChannel> = .http1(),
configuration: HBApplicationConfiguration = HBApplicationConfiguration(),
eventLoopGroupProvider: EventLoopGroupProvider = .singleton
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import Logging
import NIOCore

/// HTTPServer child channel setup protocol
public protocol HBChannelSetup: Sendable {
public protocol HBChildChannel: Sendable {
associatedtype Value: Sendable

/// Initialize channel
/// - Parameters:
/// - channel: channel
/// - childHandlers: Channel handlers to add
/// - configuration: server configuration
func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value>
func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value>

/// handle async channel
func handle(value: Value, logger: Logger) async
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdCore/Server/HTTP/HTTP1Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import NIOCore
import NIOHTTPTypes
import NIOHTTPTypesHTTP1

public struct HTTP1Channel: HBChannelSetup, HTTPChannelHandler {
public struct HTTP1Channel: HBChildChannel, HTTPChannelHandler {
public typealias Value = NIOAsyncChannel<HTTPRequestPart, HTTPResponsePart>

public init(
Expand All @@ -29,7 +29,7 @@ public struct HTTP1Channel: HBChannelSetup, HTTPChannelHandler {
self.responder = responder
}

public func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value> {
public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value> {
let childChannelHandlers: [any ChannelHandler] =
[HTTP1ToHTTPServerCodec(secure: false)] +
self.additionalChannelHandlers() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
import NIOCore

/// Build Channel Setup that takes an HTTP responder
public struct HBHTTPChannelSetupBuilder<ChannelSetup: HBChannelSetup>: Sendable {
public let build: @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChannelSetup
public init(_ build: @escaping @Sendable (@escaping HTTPChannelHandler.Responder) throws -> ChannelSetup) {
public struct HBHTTPChannelBuilder<ChildChannel: HBChildChannel>: 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<HTTP1Channel> {
) -> HBHTTPChannelBuilder<HTTP1Channel> {
return .init { responder in
return HTTP1Channel(responder: responder, additionalChannelHandlers: additionalChannelHandlers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/HummingbirdCore/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import NIOTransportServices
import ServiceLifecycle

/// HTTP server class
public actor HBServer<ChannelSetup: HBChannelSetup>: Service {
public typealias AsyncChildChannel = ChannelSetup.Value
public actor HBServer<ChildChannel: HBChildChannel>: Service {
public typealias AsyncChildChannel = ChildChannel.Value
public typealias AsyncServerChannel = NIOAsyncChannel<AsyncChildChannel, Never>
enum State: CustomStringConvertible {
case initial(
childChannelSetup: ChannelSetup,
childChannelSetup: ChildChannel,
configuration: HBServerConfiguration,
onServerRunning: (@Sendable (Channel) async -> Void)?
)
Expand Down Expand Up @@ -72,7 +72,7 @@ public actor HBServer<ChannelSetup: HBChannelSetup>: 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,
Expand Down Expand Up @@ -175,7 +175,7 @@ public actor HBServer<ChannelSetup: HBChannelSetup>: 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) {
Expand Down Expand Up @@ -204,7 +204,7 @@ public actor HBServer<ChannelSetup: HBChannelSetup>: Service {
port: port,
serverBackPressureStrategy: nil
) { channel in
childChannelSetup.initialize(
childChannelSetup.setup(
channel: channel,
configuration: configuration,
logger: self.logger
Expand All @@ -217,7 +217,7 @@ public actor HBServer<ChannelSetup: HBChannelSetup>: Service {
cleanupExistingSocketFile: false,
serverBackPressureStrategy: nil
) { channel in
childChannelSetup.initialize(
childChannelSetup.setup(

Check warning on line 220 in Sources/HummingbirdCore/Server/Server.swift

View check run for this annotation

Codecov / codecov/patch

Sources/HummingbirdCore/Server/Server.swift#L220

Added line #L220 was not covered by tests
channel: channel,
configuration: configuration,
logger: self.logger
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdHTTP2/HTTP2Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value> {
public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value> {
do {
try channel.pipeline.syncOperations.addHandler(NIOSSLServerHandler(context: self.sslContext))
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTP2Channel> {
) throws -> HBHTTPChannelBuilder<HTTP2Channel> {
return .init { responder in
return try HTTP2Channel(
tlsConfiguration: tlsConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import NIOCore
import NIOSSL

/// Sets up child channel to use TLS before accessing base channel setup
public struct TLSChannel<BaseChannel: HBChannelSetup>: HBChannelSetup {
public struct TLSChannel<BaseChannel: HBChildChannel>: HBChildChannel {
public typealias Value = BaseChannel.Value

public init(_ baseChannel: BaseChannel, tlsConfiguration: TLSConfiguration) throws {
Expand All @@ -13,9 +13,9 @@ public struct TLSChannel<BaseChannel: HBChannelSetup>: HBChannelSetup {
}

@inlinable
public func initialize(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value> {
public func setup(channel: Channel, configuration: HBServerConfiguration, logger: Logger) -> EventLoopFuture<Value> {
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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import HummingbirdCore
import NIOSSL

extension HBHTTPChannelSetupBuilder {
public static func tls<BaseChannel: HBChannelSetup>(
_ base: HBHTTPChannelSetupBuilder<BaseChannel> = .http1(),
extension HBHTTPChannelBuilder {
public static func tls<BaseChannel: HBChildChannel>(
_ base: HBHTTPChannelBuilder<BaseChannel> = .http1(),
tlsConfiguration: TLSConfiguration
) throws -> HBHTTPChannelSetupBuilder<TLSChannel<BaseChannel>> {
) throws -> HBHTTPChannelBuilder<TLSChannel<BaseChannel>> {
return .init { responder in
return try TLSChannel(base.build(responder), tlsConfiguration: tlsConfiguration)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdXCT/HBXCTLive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ final class HBXCTLive<App: HBApplicationProtocol>: HBXCTApplication {
/// TestApplication used to wrap HBApplication being tested
struct TestApplication<BaseApp: HBApplicationProtocol>: HBApplicationProtocol, Service {
typealias Responder = BaseApp.Responder
typealias ChannelSetup = BaseApp.ChannelSetup
typealias ChildChannel = BaseApp.ChildChannel

let base: BaseApp

var responder: Responder {
get async throws { try await self.base.responder }
}

var channelSetup: HBHTTPChannelSetupBuilder<ChannelSetup> {
var channelSetup: HBHTTPChannelBuilder<ChildChannel> {
self.base.channelSetup
}

Expand Down
14 changes: 7 additions & 7 deletions Tests/HummingbirdCoreTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public enum TestErrors: Error {
}

/// Helper function for testing a server
public func testServer<ChannelSetup: HBChannelSetup, Value: Sendable>(
public func testServer<ChildChannel: HBChildChannel, Value: Sendable>(
responder: @escaping HTTPChannelHandler.Responder,
httpChannelSetup: HBHTTPChannelSetupBuilder<ChannelSetup>,
httpChannelSetup: HBHTTPChannelBuilder<ChildChannel>,
configuration: HBServerConfiguration,
eventLoopGroup: EventLoopGroup,
logger: Logger,
_ test: @escaping @Sendable (HBServer<ChannelSetup>, Int) async throws -> Value
_ test: @escaping @Sendable (HBServer<ChildChannel>, Int) async throws -> Value
) async throws -> Value {
try await withThrowingTaskGroup(of: Void.self) { group in
let promise = Promise<Int>()
Expand Down Expand Up @@ -68,14 +68,14 @@ public func testServer<ChannelSetup: HBChannelSetup, Value: Sendable>(
///
/// Creates test client, runs test function abd ensures everything is
/// shutdown correctly
public func testServer<ChannelSetup: HBChannelSetup, Value: Sendable>(
public func testServer<ChildChannel: HBChildChannel, Value: Sendable>(
responder: @escaping HTTPChannelHandler.Responder,
httpChannelSetup: HBHTTPChannelSetupBuilder<ChannelSetup>,
httpChannelSetup: HBHTTPChannelBuilder<ChildChannel>,
configuration: HBServerConfiguration,
eventLoopGroup: EventLoopGroup,
logger: Logger,
clientConfiguration: HBXCTClient.Configuration = .init(),
_ test: @escaping @Sendable (HBServer<ChannelSetup>, HBXCTClient) async throws -> Value
_ test: @escaping @Sendable (HBServer<ChildChannel>, HBXCTClient) async throws -> Value
) async throws -> Value {
try await withThrowingTaskGroup(of: Void.self) { group in
let promise = Promise<Int>()
Expand Down Expand Up @@ -112,7 +112,7 @@ public func testServer<ChannelSetup: HBChannelSetup, Value: Sendable>(

public func testServer<Value: Sendable>(
responder: @escaping HTTPChannelHandler.Responder,
httpChannelSetup: HBHTTPChannelSetupBuilder<some HBChannelSetup> = .http1(),
httpChannelSetup: HBHTTPChannelBuilder<some HBChildChannel> = .http1(),
configuration: HBServerConfiguration,
eventLoopGroup: EventLoopGroup,
logger: Logger,
Expand Down

0 comments on commit 1ae5bb5

Please sign in to comment.