This commit is contained in:
k00kzaAntonovaIra
2026-11-22 19:55:50 +03:00
parent 470d9d6f1e
commit 2d10a9b788
186 changed files with 15732 additions and 425 deletions
+38
View File
@@ -0,0 +1,38 @@
"""Идентификатор API-реплики и заголовок X-Instance-Id."""
import os
from uuid import uuid4
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
INSTANCE_ID_HEADER = "X-Instance-Id"
def resolve_instance_id(configured: str | None) -> str:
if configured and configured.strip():
return configured.strip()
hostname = os.getenv("HOSTNAME", "").strip()
if hostname:
return hostname
return str(uuid4())
class InstanceIdMiddleware(BaseHTTPMiddleware):
def __init__(self, app, instance_id: str) -> None:
super().__init__(app)
self.instance_id = instance_id
async def dispatch(
self,
request: Request,
call_next: RequestResponseEndpoint,
) -> Response:
response = await call_next(request)
response.headers[INSTANCE_ID_HEADER] = self.instance_id
return response