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
log | logging.Logger | The main application logger. |
config | Config | Application configuration object. |
router | Router | The URL router responsible for dispatching requests. |
error_handler | ErrorHandler | Handles exceptions raised during request processing. |
workdir | Path | 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
self | Ushka | 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
host | str | The host IP address to bind to. (Default is "127.0.0.1"). |
port | int | The port number to listen on. (Default is 8000). |
log_level | LogLevelType | 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
url | str | URL prefix, e.g. |
directory | str or Path | Folder to serve; relative paths are resolved against the app directory. |
html | bool | Serve |
cache_control | str |
|
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
path | str | 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
path | str | 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
path | str | 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
path | str | 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
path | str | The URL path template. |
Returns
callable | The route registration decorator. |