-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change RouterMethods requirements slightly Move all path manipulation in Router/RouterGroup to implementation of this function * Added RouteCollection Edited RouterGroup so it can be used with RouteCollection * Add tests for RouteCollections * Use flat array instead of dictionary to store routes * Update Sources/Hummingbird/Router/RouteCollection.swift Co-authored-by: Joannis Orlandos <[email protected]> * Update tests --------- Co-authored-by: Joannis Orlandos <[email protected]>
- Loading branch information
1 parent
f1e6d1c
commit ce5d492
Showing
5 changed files
with
214 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 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 HTTPTypes | ||
|
||
/// Collection of routes | ||
public final class RouteCollection<Context: BaseRequestContext>: RouterMethods { | ||
/// Initialize RouteCollection | ||
public init(context: Context.Type = BasicRequestContext.self) { | ||
self.routes = .init() | ||
self.middlewares = .init() | ||
} | ||
|
||
/// Add responder to call when path and method are matched | ||
/// | ||
/// - Parameters: | ||
/// - path: Path to match | ||
/// - method: Request method to match | ||
/// - responder: Responder to call if match is made | ||
/// - Returns: self | ||
public func on<Responder: HTTPResponder>( | ||
_ path: String, | ||
method: HTTPRequest.Method, | ||
responder: Responder | ||
) -> Self where Responder.Context == Context { | ||
let route = RouteDefinition(path: path, method: method, responder: responder) | ||
self.routes.append(route) | ||
return self | ||
} | ||
|
||
/// Return a group inside the route collection | ||
/// - Parameter path: path prefix to add to routes inside this group | ||
public func group(_ path: String = "") -> RouterGroup<Context> { | ||
return .init(path: path, router: self) | ||
} | ||
|
||
/// Add middleware to RouteCollection | ||
@discardableResult public func add(middleware: any RouterMiddleware<Context>) -> Self { | ||
self.middlewares.add(middleware) | ||
return self | ||
} | ||
|
||
fileprivate struct RouteDefinition { | ||
let path: String | ||
let method: HTTPRequest.Method | ||
let responder: any HTTPResponder<Context> | ||
} | ||
|
||
fileprivate var routes: [RouteDefinition] | ||
let middlewares: MiddlewareGroup<Context> | ||
} | ||
|
||
extension RouterMethods { | ||
/// Add route collection to router | ||
/// - Parameter collection: Route collection | ||
public func addRoutes(_ collection: RouteCollection<Context>, atPath path: String = "") { | ||
for route in collection.routes { | ||
// ensure path starts with a "/" and doesn't end with a "/" | ||
let path = self.combinePaths(path, route.path) | ||
self.on(path, method: route.method, responder: collection.middlewares.constructResponder(finalResponder: route.responder)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters