Skip to content

Commit

Permalink
Allow specialization of the RouterResponder
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed May 9, 2024
1 parent 12dc8e3 commit 49fe5c8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
7 changes: 6 additions & 1 deletion Sources/Hummingbird/Router/EndpointResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import HTTPTypes

/// Stores endpoint responders for each HTTP method
@usableFromInline
struct EndpointResponders<Context: BaseRequestContext>: Sendable {
init(path: String) {
self.path = path
self.methods = [:]
}

public func getResponder(for method: HTTPRequest.Method) -> (any HTTPResponder<Context>)? {
@inlinable
public func getResponder(for method: __shared HTTPRequest.Method) -> (any HTTPResponder<Context>)? {
return self.methods[method]
}

Expand All @@ -41,6 +43,9 @@ struct EndpointResponders<Context: BaseRequestContext>: Sendable {
}
}

@usableFromInline
var methods: [HTTPRequest.Method: any HTTPResponder<Context>]

@usableFromInline
var path: String
}
6 changes: 6 additions & 0 deletions Sources/Hummingbird/Router/RouterResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import NIOCore

public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
@usableFromInline
let trie: RouterTrie<EndpointResponders<Context>>

@usableFromInline
let notFoundResponder: any HTTPResponder<Context>

@usableFromInline
let options: RouterOptions

init(
Expand All @@ -33,6 +38,7 @@ public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
/// Respond to request by calling correct handler
/// - Parameter request: HTTP request
/// - Returns: EventLoopFuture that will be fulfilled with the Response
@inlinable
public func respond(to request: Request, context: Context) async throws -> Response {
let path: String
if self.options.contains(.caseInsensitive) {
Expand Down
12 changes: 6 additions & 6 deletions Sources/Hummingbird/Router/Trie/RouterTrie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ struct TrieNode: Sendable {
struct Trie: Sendable {
@usableFromInline
var nodes = [TrieNode]()

@usableFromInline
var parameters = [Substring]()

@usableFromInline
var constants = [Substring]()

@usableFromInline
init() {}
}

@_spi(Internal) public final class RouterTrie<Value: Sendable>: Sendable {
@usableFromInline
internal final class RouterTrie<Value: Sendable>: Sendable {
@usableFromInline
let trie: Trie

@usableFromInline
let values: [Value?]

@inlinable
@_spi(Internal) public init(base: RouterPathTrieBuilder<Value>) {
internal init(base: RouterPathTrieBuilder<Value>) {
var trie = Trie()
var values: [Value?] = []

Expand Down
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Router/Trie/Trie+resolve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import NIOCore

extension RouterTrie {
/// Resolve a path to a `Value` if available
@inlinable
@_spi(Internal) public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? {
@usableFromInline
internal func resolve(_ path: String) -> (value: Value, parameters: Parameters)? {
let pathComponents = path.split(separator: "/", omittingEmptySubsequences: true)
var pathComponentsIterator = pathComponents.makeIterator()
var parameters = Parameters()
Expand Down
3 changes: 0 additions & 3 deletions Sources/Hummingbird/Router/Trie/Trie+serialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import NIOCore

extension RouterTrie {
@usableFromInline
static func serialize(
_ node: RouterPathTrieBuilder<Value>.Node,
trie: inout Trie,
Expand Down Expand Up @@ -92,7 +91,6 @@ extension RouterTrie {
trie.nodes[nodeIndex].nextSiblingNodeIndex = trie.nodes.count
}

@usableFromInline
static func serializeChildren(
of node: RouterPathTrieBuilder<Value>.Node,
trie: inout Trie,
Expand All @@ -105,7 +103,6 @@ extension RouterTrie {
}
}

@usableFromInline
internal static func highestPriorityFirst(lhs: RouterPathTrieBuilder<Value>.Node, rhs: RouterPathTrieBuilder<Value>.Node) -> Bool {
lhs.key.priority > rhs.key.priority
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Router/TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import HummingbirdCore
}
}

@_spi(Internal) public func build() -> RouterTrie<Value> {
internal func build() -> RouterTrie<Value> {
.init(base: self)
}

Expand Down

0 comments on commit 49fe5c8

Please sign in to comment.