On this page
Path-based HTTP router with curly-brace parameter placeholders, automatic type coercion, method-based dispatch, and route group composition.
#src.fastware.routing
#src.fastware.routing
Path-based HTTP router with curly-brace parameter placeholders, automatic type coercion, method-based dispatch, and route group composition.
#_parse_segment
def _parse_segment(seg: str) -> tuple[str | None, str | None, type | None]Parse a route pattern segment into (literal, param_name, converter).
Returns one of:
- (literal_str, None, None) for plain segments like "api"
- (None, param_name, converter) for parameterized segments like {id:int}
- (None, param_name, None) for :path segments (greedy)
#Router
Simple path-based HTTP router using {param} placeholders and type coercion.
Supports {param}, {param:str}, {param:int}, and {param:path} syntax.
#mount
def mount(self, prefix: str, app: Any) -> NoneMount an ASGI sub-application at a path prefix.
When a request path starts with prefix, the scope is rewritten (path stripped, root_path extended) and forwarded to app. Both http and websocket scope types are forwarded.
The prefix must start with / and must not end with /. A trailing slash is stripped automatically.
#get
def get(self, path: str, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> CallableDecorator to register a GET handler.
#post
def post(self, path: str, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> CallableDecorator to register a POST handler.
#delete
def delete(self, path: str, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> CallableDecorator to register a DELETE handler.
#put
def put(self, path: str, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> CallableDecorator to register a PUT handler.
#patch
def patch(self, path: str, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> CallableDecorator to register a PATCH handler.
#add_route
def add_route(self, method: str, path: str, handler: Callable, *, deps: dict[str, Callable] | None=None, response_model: type | None=None) -> NoneProgrammatic route registration.
#ws
def ws(self, path: str, *, deps: dict[str, Callable] | None=None) -> CallableDecorator to register a WebSocket handler.
#add_ws_route
def add_ws_route(self, path: str, handler: Callable, *, deps: dict[str, Callable] | None=None) -> NoneRegister a WebSocket handler for a path pattern (supports {param}).
#include_router
def include_router(self, other: Router, prefix: str | None=None, deps: dict[str, Callable] | None=None) -> NoneCopy all routes from other into this router.
If prefix is given (e.g. "/api/v1"), its segments are prepended to every copied route's pattern.
If deps is given (a dict mapping names to factory callables), they are merged into each copied route's deps. Router-level deps are listed first so that per-handler deps can override them.
#match
def match(self, method: str, path: str) -> tuple[Callable, dict[str, Any]] | NoneReturn (handler, path_params) or None if no route matches.
Path parameter values are coerced to their declared types (e.g., {id:int} produces an int). If coercion fails the route does not match, allowing fall-through to 404.
#_match_with_deps
def _match_with_deps(self, method: str, path: str) -> tuple[Callable, dict[str, Any], dict[str, Callable], type | None] | NoneReturn (handler, path_params, deps, response_model) or None.
Internal variant of :meth:match that also returns the merged dependency dict and response_model for the matched route. Used by create_app for DI resolution and response validation.
#_match_with_path_param
def _match_with_path_param(pattern: list[ParsedSegment], segments: list[str], path_idx: int) -> dict[str, Any] | NoneMatch a route pattern containing a :path greedy parameter.
Literal/typed segments before the :path param must match exactly. Literal/typed segments after the :path param are matched from the end of the path. Everything in between is consumed by the :path parameter (joined with "/").
#match_ws
def match_ws(self, path: str) -> tuple[Callable, dict[str, Any]] | NoneReturn (handler, path_params) for a WebSocket path, or None.
#_match_ws_with_deps
def _match_ws_with_deps(self, path: str) -> tuple[Callable, dict[str, Any], dict[str, Callable]] | NoneReturn (handler, path_params, deps) for a WebSocket path, or None.
Internal variant of :meth:match_ws that also returns deps.