ushka.core.router
This module defines the Router class, which acts as the central route manager
for the Ushka framework.
It is responsible for mapping incoming URL requests to their corresponding Python handler functions, managing both static and dynamic routes, and handling dependency injection for route handlers.
class Router
Central route manager.
Responsible for linking URLs to Python functions (handlers) and managing dependency injection for those handlers.
Parameters
log | logging.Logger | The application logger instance. |
workdir | Path | The root working directory of the application, used for route discovery. |
host | str | The default host address (e.g., '127.0.0.1'). Defaults to "127.0.0.1". |
Attributes
HTTP_METHODS | Set[str] | A set of valid HTTP verbs supported by the router. |
static_routes | Dict[str, Dict[str, Tuple[RouteHandler, DependencyMap]]] | Storage for routes without path parameters (method -> path -> (handler, deps)). |
dynamic_routes | Dict[str, List[Tuple[re.Pattern, List[str], RouteHandler, DependencyMap, str]]] | Storage for routes with path parameters, requiring regex matching. |
__init__(self, log: logging.Logger, workdir: Path, host: str = '127.0.0.1')
Initializes the Router with a logger, working directory, and host.
Sets up internal data structures for storing static and dynamic routes.
Parameters
log | logging.Logger | The application logger instance. |
workdir | Path | The root working directory of the application, used for route discovery. |
host | str | The default host address (e.g., '127.0.0.1'). Defaults to "127.0.0.1". |
add_route(self, method: str, path: str, func: RouteHandler) -> None
Registers a manual or discovered route.
Parameters
method | str | The HTTP method (e.g., 'GET', 'POST'). Case-insensitive. |
path | str | The URL path (e.g., '/users', '/users/[id]'). |
func | RouteHandler | The Python function to execute when the route is matched. |
Returns
None |
get_route(self, request: Request) -> Tuple[Optional[RouteHandler], Dict[str, Any]]
Searches for the route matching the request method and path.
It prioritizes static routes over dynamic routes.
Parameters
request | Request | The incoming HTTP request object. |
Returns
Tuple[Optional[RouteHandler], Dict[str, Any]] | A tuple containing:
|
allowed_methods(self, path: str) -> List[str]
Returns the methods registered for path (used to answer 405).
autodiscover(self, folder: str = 'routes') -> None
Filesystem-based routing discovery.
Reads files in the specified folder (relative to workdir) and maps
functions named after HTTP methods (e.g., get, post) to URLs
derived from the file path.
Parameters
folder | str | The subdirectory name containing route files. Defaults to "routes". |
Returns
None |
get_urls(self, host: str = '127.0.0.1', port: int = 8000, with_host: bool = True) -> List[str]
Generates a list of registered routes for display in the terminal.
Parameters
host | str | The host address to prepend to the URLs. Defaults to "127.0.0.1". |
port | int | The port number to prepend to the URLs. Defaults to 8000. |
with_host | bool | If True, includes the 'http://host:port' prefix. Defaults to True. |
Returns
List[str] | A sorted list of strings, where each string represents a route and its supported methods (e.g., 'http://127.0.0.1:8000/users - GET, POST'). |