How vapor/routing-kit works
Project type and purpose
• This is a Swift Package library (“RoutingKit”), not a standalone app.
• Its sole purpose is to provide a high-performance HTTP request router (trie-based) that frameworks (e.g., Vapor) can plug into.
Main components of the system
• Public API layer
– AnyRouter: type-erased, user-facing protocol conformer.
– Router protocol: defines core routing interface (e.g., insert(path:handler:), route(method:path:)).
• Core engine
– TrieRouter: concrete, high-performance trie implementation of Router.
– PathComponent: enum representing static segments, parameters, wildcards.
– Parameters: struct holding dynamic values extracted during routing.
• Package and Documentation
– Package.swift: Swift Package manifest (defines module, targets).
– Docs.docc: documentation bundle for DocC (resources, index.md, theme settings).
• Support modules
– Tests/RoutingKitTests: unit tests validating routing behavior.
– Benchmarks: performance measurement targets (RouterPerformance).
Relationships and data flow
1. An HTTP server (or web framework) receives a request (method, full path).
2. It invokes the public API (AnyRouter or Router protocol) to find a matching handler:
HTTPServer → AnyRouter (type-erased) → Router protocol → TrieRouter.match(...)
3. TrieRouter walks its internal Trie of PathComponent nodes to find the correct handler.
4. During the walk, dynamic path parameters are collected into a Parameters instance.
5. The handler and extracted Parameters are returned back up the chain to the HTTP server/framework.
Architectural patterns and design principles
• Protocol-oriented design: Router protocol abstracts routing logic; implementations can vary.
• Type erasure: AnyRouter hides concrete implementation details from consumers.
• Trie (prefix tree) data structure: enables O(m) lookup (m = number of path segments).
• Modular separation: routing core separate from tests, benchmarks, and documentation.