Files
fastapi_best_architecture/backend/common/context.py
Wu Clan 6a065797f5 Update request state usage to context variable (#853)
* Update request state usage to context variable

* Update context to custom ctx

* Fix the exception interception in opera log

* restore elapsed
2025-10-16 10:23:39 +08:00

33 lines
642 B
Python

from datetime import datetime
from typing import Any, Protocol
from starlette_context.ctx import _Context, context
class TypedContextProtocol(Protocol):
perf_time: float
start_time: datetime
ip: str
country: str | None
region: str | None
city: str | None
user_agent: str
os: str | None
browser: str | None
device: str | None
permission: str | None
class TypedContext(TypedContextProtocol, _Context):
def __getattr__(self, name: str) -> Any:
return context.get(name)
def __setattr__(self, name: str, value: Any) -> None:
context[name] = value
ctx = TypedContext()