Ushka

API reference

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

loglogging.Logger

The application logger instance.

workdirPath

The root working directory of the application, used for route discovery.

hoststr

The default host address (e.g., '127.0.0.1'). Defaults to "127.0.0.1".

Attributes

HTTP_METHODSSet[str]

A set of valid HTTP verbs supported by the router.

static_routesDict[str, Dict[str, Tuple[RouteHandler, DependencyMap]]]

Storage for routes without path parameters (method -> path -> (handler, deps)).

dynamic_routesDict[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

loglogging.Logger

The application logger instance.

workdirPath

The root working directory of the application, used for route discovery.

hoststr

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

methodstr

The HTTP method (e.g., 'GET', 'POST'). Case-insensitive.

pathstr

The URL path (e.g., '/users', '/users/[id]').

funcRouteHandler

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

requestRequest

The incoming HTTP request object.

Returns

Tuple[Optional[RouteHandler], Dict[str, Any]]

A tuple containing:

  1. The matching handler function (or None if not found).
  2. A dictionary of keyword arguments, including injected dependencies and path parameters.

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

folderstr

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

hoststr

The host address to prepend to the URLs. Defaults to "127.0.0.1".

portint

The port number to prepend to the URLs. Defaults to 8000.

with_hostbool

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').