Router
Create rules for routing requests and then create Responder
that will follow these rules.
final class Router<Context> where Context : RequestContext
Router
requires an implementation of the on(path:method:use)
functions but because it also conforms to RouterMethods
it is also possible to call the method specific functions get
, put
, head
, post
and patch
. The route handler closures all return objects conforming to ResponseGenerator
. This allows us to support routes which return a multitude of types eg
router.get("string") { _, _ -> String in
return "string"
}
router.post("status") { _, _ -> HTTPResponse.Status in
return .ok
}
router.data("data") { request, context -> ByteBuffer in
return ByteBuffer(string: "buffer")
}
The default Router
setup in Application
is the TrieRouter
. This uses a trie to partition all the routes for faster access. It also supports wildcards and parameter extraction
router.get("user/*", use: anyUser)
router.get("user/:id", use: userWithId)
Both of these match routes which start with “/user” and the next path segment being anything. The second version extracts the path segment out and adds it to Request.parameters
with the key “id”.
Citizens in Hummingbird
Conformances
protocol HTTPResponderBuilder
A type that has a single method to build a HTTPResponder
protocol RouterMethods<Context>
Conform to
RouterMethods
to add standard router verb (get, post …) methods
Type members
Instance members
let middlewares: MiddlewareGroup<Context>
func add(middleware: any MiddlewareProtocol<Request, Response, Context>
) -> Self Add middleware to Router
func buildResponder(
) -> RouterResponder<Context> build responder from router
func on<Responder>(RouterPath, method: HTTPRequest.Method, responder: Responder
) -> Self Add responder to call when path and method are matched
Instance features
func addMiddleware(buildMiddlewareStack: () -> some MiddlewareProtocol<Request, Response, Context>
) -> Self Add middleware stack to router
func addRoutes(RouteCollection<Context>, atPath: RouterPath
) -> Self Add route collection to router
func delete(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self DELETE path for async closure returning type conforming to ResponseGenerator
func get(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self GET path for async closure returning type conforming to ResponseGenerator
func group(RouterPath
) -> RouterGroup<Context> Return a group inside the current group
func group<TargetContext>(RouterPath, context: TargetContext.Type
) -> RouterGroup<TargetContext> Return a group inside the current group that transforms the
RequestContext
func head(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self HEAD path for async closure returning type conforming to ResponseGenerator
func on(RouterPath, method: HTTPRequest.Method, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self Add path for async closure
func patch(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self PATCH path for async closure returning type conforming to ResponseGenerator
func post(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self POST path for async closure returning type conforming to ResponseGenerator
func put(RouterPath, use: @escaping (Request, Context) async throws -> some ResponseGenerator
) -> Self PUT path for async closure returning type conforming to ResponseGenerator