Ushka

API reference

ushka.core.app

This module defines the core Ushka application class, which serves as the central entry point for handling ASGI requests, managing routing, configuration, and error handling within the framework.

class Ushka

Core Application Class.

Centralizes the ASGI lifecycle, routing, and execution.

Attributes

loglogging.Logger

The main application logger.

configConfig

Application configuration object.

routerRouter

The URL router responsible for dispatching requests.

error_handlerErrorHandler

Handles exceptions raised during request processing.

workdirPath

The root directory of the application.

__init__(self) -> None

Initializes the Ushka application.

Sets up the application's working directory, loads configuration from ushka.toml, and initializes core components such as logging, routing, and error handling. It also registers static file serving routes if enabled in the application's configuration.

Parameters

selfUshka

The instance of the Ushka application.

run(self, host: str = '', port: int = 0, log_level: LogLevelType = 'INFO')

Starts the Uvicorn server, running the Ushka application instance.

Parameters

hoststr

The host IP address to bind to. (Default is "127.0.0.1").

portint

The port number to listen on. (Default is 8000).

log_levelLogLevelType

The logging level for the server (e.g., "INFO", "DEBUG"). (Default is "INFO").

mount_static(self, url: str, directory: str | Path, html: bool = False, cache_control: str = DEFAULT_CACHE_CONTROL) -> None

Serves the files in directory under url.

Parameters

urlstr

URL prefix, e.g. "/docs".

directorystr or Path

Folder to serve; relative paths are resolved against the app directory.

htmlbool

Serve index.html for directories and redirect /docs/guide to /docs/guide/ so relative links work (static-site output).

cache_controlstr

Cache-Control for every file, e.g. "public, max-age=31536000, immutable" for fingerprinted assets.

Notes

Every request path is validated with ushka.guard.safe.path; files get an ETag and revalidation answers 304 Not Modified.

on_startup(self, func)

Registers a function (sync or async) to run when the server starts.

Use it to open connection pools, warm caches, etc. If it raises, the server refuses to start and logs the error.

on_shutdown(self, func)

Registers a function (sync or async) to run when the server stops.

get(self, path: str)

Decorator to register a function as a GET route handler.

Parameters

pathstr

The URL path template.

Returns

callable

The route registration decorator.

post(self, path: str)

Decorator to register a function as a POST route handler.

Parameters

pathstr

The URL path template.

Returns

callable

The route registration decorator.

put(self, path: str)

Decorator to register a function as a PUT route handler.

Parameters

pathstr

The URL path template.

Returns

callable

The route registration decorator.

patch(self, path: str)

Decorator to register a function as a PATCH route handler.

Parameters

pathstr

The URL path template.

Returns

callable

The route registration decorator.

delete(self, path: str)

Decorator to register a function as a DELETE route handler.

Parameters

pathstr

The URL path template.

Returns

callable

The route registration decorator.