Files
mtusi2026lab5/app/runtime.py
T

38 lines
1.1 KiB
Python

"""Shared runtime objects created at process startup."""
from __future__ import annotations
from argon2 import PasswordHasher
from argon2.low_level import Type
from app.broker import Publisher
from app.settings import Settings
from app.s3store import ObjectStore
settings: Settings | None = None
store: ObjectStore | None = None
publisher: Publisher | None = None
hasher = PasswordHasher(
time_cost=2,
memory_cost=19456,
parallelism=1,
hash_len=32,
salt_len=16,
type=Type.ID,
)
dummy_hash: str | None = None
def init(app_settings: Settings, object_store: ObjectStore, message_publisher: Publisher) -> None:
global settings, store, publisher, dummy_hash
settings = app_settings
store = object_store
publisher = message_publisher
if dummy_hash is None:
dummy_hash = hasher.hash("dummy-password-for-equal-time")
def require() -> tuple[Settings, ObjectStore, Publisher]:
if settings is None or store is None or publisher is None:
raise RuntimeError("runtime is not initialized")
return settings, store, publisher