Skip to content

Commit

Permalink
Playing with naming: rename InstantiableRequestContext to Initializab…
Browse files Browse the repository at this point in the history
…leFromSource and set Source as primary type (#475)

* InstantiableRequestContext -> InitializedFrom

* Moved stuff about

* SourceInitializable

* InitializableFromSource

* Update file name
  • Loading branch information
adam-fowler authored Jun 17, 2024
1 parent 577873b commit 1d10ee0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum EventLoopGroupProvider {
}

/// Protocol for an Application. Brings together all the components of Hummingbird together
public protocol ApplicationProtocol: Service where Context: InstantiableRequestContext, Context.Source == ServerRequestContextSource {
public protocol ApplicationProtocol: Service where Context: InitializableFromSource<ApplicationRequestContextSource> {
/// Responder that generates a response from a requests and context
associatedtype Responder: HTTPResponder
/// Context passed with Request to responder
Expand Down Expand Up @@ -173,7 +173,7 @@ extension ApplicationProtocol {
/// try await app.runService()
/// ```
/// Editing the application setup after calling `runService` will produce undefined behaviour.
public struct Application<Responder: HTTPResponder>: ApplicationProtocol where Responder.Context: InstantiableRequestContext, Responder.Context.Source == ServerRequestContextSource {
public struct Application<Responder: HTTPResponder>: ApplicationProtocol where Responder.Context: InitializableFromSource<ApplicationRequestContextSource> {
// MARK: Member variables

/// event loop group used by application
Expand Down
33 changes: 3 additions & 30 deletions Sources/Hummingbird/Server/RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2023 the Hummingbird authors
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -34,26 +34,6 @@ public struct EndpointPath: Sendable {
private let _value: NIOLockedValueBox<String?>
}

/// Protocol for request context source
public protocol RequestContextSource {
/// ByteBuffer allocator
var allocator: ByteBufferAllocator { get }
/// Request Logger
var logger: Logger { get }
}

/// RequestContext source for server applications
public struct ServerRequestContextSource: RequestContextSource {
public init(channel: any Channel, logger: Logger) {
self.channel = channel
self.logger = logger
}

public let channel: Channel
public let logger: Logger
public var allocator: ByteBufferAllocator { self.channel.allocator }
}

/// Request context values required by Hummingbird itself.
public struct CoreRequestContextStorage: Sendable {
/// ByteBuffer allocator used by request
Expand All @@ -78,17 +58,10 @@ public struct CoreRequestContextStorage: Sendable {
}
}

/// A RequestContext that can be built from some source
public protocol InstantiableRequestContext: Sendable {
associatedtype Source
/// Initialise RequestContext from source
init(source: Source)
}

/// Protocol that all request contexts should conform to. Holds data associated with
/// a request. Provides context for request processing
public protocol RequestContext: InstantiableRequestContext {
associatedtype Source: RequestContextSource = ServerRequestContextSource
public protocol RequestContext: InitializableFromSource {
associatedtype Source: RequestContextSource = ApplicationRequestContextSource
associatedtype Decoder: RequestDecoder = JSONDecoder
associatedtype Encoder: ResponseEncoder = JSONEncoder

Expand Down
36 changes: 36 additions & 0 deletions Sources/Hummingbird/Server/RequestContextSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Logging
import NIOCore

/// Protocol for source of request contexts
public protocol RequestContextSource {
/// ByteBuffer allocator
var allocator: ByteBufferAllocator { get }
/// Request Logger
var logger: Logger { get }
}

/// RequestContext source for contexts created by ``Application``.
public struct ApplicationRequestContextSource: RequestContextSource {
public init(channel: any Channel, logger: Logger) {
self.channel = channel
self.logger = logger
}

public let channel: Channel
public let logger: Logger
public var allocator: ByteBufferAllocator { self.channel.allocator }
}
20 changes: 20 additions & 0 deletions Sources/Hummingbird/Utils/InitializableFromSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

/// A type that can be initialized from another type
public protocol InitializableFromSource<Source>: Sendable {
associatedtype Source
/// Initialise RequestContext from source
init(source: Source)
}
4 changes: 2 additions & 2 deletions Sources/HummingbirdTesting/RouterTestFramework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import NIOPosix
import ServiceLifecycle

/// Test sending requests directly to router. This does not setup a live server
struct RouterTestFramework<Responder: HTTPResponder>: ApplicationTestFramework where Responder.Context: InstantiableRequestContext {
struct RouterTestFramework<Responder: HTTPResponder>: ApplicationTestFramework where Responder.Context: InitializableFromSource {
let responder: Responder
let makeContext: @Sendable (Logger) -> Responder.Context
let services: [any Service]
let logger: Logger
let processesRunBeforeServerStart: [@Sendable () async throws -> Void]

init<App: ApplicationProtocol>(app: App) async throws where App.Responder == Responder, Responder.Context: InstantiableRequestContext {
init<App: ApplicationProtocol>(app: App) async throws where App.Responder == Responder, Responder.Context: InitializableFromSource {
self.responder = try await app.responder
self.processesRunBeforeServerStart = app.processesRunBeforeServerStart
self.services = app.services
Expand Down
5 changes: 2 additions & 3 deletions Tests/HummingbirdTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,8 @@ final class ApplicationTests: XCTestCase {

/// test we can create an application that accepts a responder with an empty context
func testEmptyRequestContext() async throws {
struct EmptyRequestContext: InstantiableRequestContext {
typealias Source = ServerRequestContextSource

struct EmptyRequestContext: InitializableFromSource {
typealias Source = ApplicationRequestContextSource
init(source: Source) {}
}
let app = Application(
Expand Down

0 comments on commit 1d10ee0

Please sign in to comment.