Skip to content

Commit

Permalink
Use flat array instead of dictionary to store routes
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Apr 22, 2024
1 parent 66b050c commit 68368af
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Sources/Hummingbird/Router/RouteCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public final class RouteCollection<Context: BaseRequestContext>: RouterMethods {
method: HTTPRequest.Method,
responder: Responder
) -> Self where Responder.Context == Context {
self.routes[.init(path: path, method: method)] = responder
let route = RouteDefinition(path: path, method: method, responder: responder)
self.routes.append(route)
return self
}

Expand All @@ -50,23 +51,24 @@ public final class RouteCollection<Context: BaseRequestContext>: RouterMethods {
return self
}

fileprivate struct RouteDefinition: Hashable {
fileprivate struct RouteDefinition {
let path: String
let method: HTTPRequest.Method
let responder: any HTTPResponder<Context>
}

fileprivate var routes: [RouteDefinition: any HTTPResponder<Context>]
fileprivate var routes: [RouteDefinition]
let middlewares: MiddlewareGroup<Context>
}

extension RouterMethods {
/// Add route collection to router
/// - Parameter collection: Route collection
public func add(_ path: String = "", routes collection: RouteCollection<Context>) {
for (definition, responder) in collection.routes {
for route in collection.routes {
// ensure path starts with a "/" and doesn't end with a "/"
let path = self.combinePaths(path, definition.path)
self.on(path, method: definition.method, responder: collection.middlewares.constructResponder(finalResponder: responder))
let path = self.combinePaths(path, route.path)
self.on(path, method: route.method, responder: collection.middlewares.constructResponder(finalResponder: route.responder))
}
}
}

0 comments on commit 68368af

Please sign in to comment.