feat: ЛР3 - Nginx балансировка, идемпотентность сообщений и failover

This commit is contained in:
Ильяс
2026-10-30 12:17:01 +03:00
parent df42648939
commit 786bb2077f
27 changed files with 943 additions and 70 deletions
+10 -3
View File
@@ -1,8 +1,7 @@
from __future__ import annotations
import re
from uuid import UUID
from fastapi import APIRouter, Depends, Query, status
from fastapi import APIRouter, Depends, Header, Query, status
from app.dependencies import get_current_user
from app.schemas import (
@@ -15,6 +14,8 @@ from app.storage import InvalidRequestError, storage
router = APIRouter(prefix="/api/v1/chats", tags=["messages"])
IDEMPOTENCY_KEY_REGEX = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
def validate_uuid(value: str, field_name: str = "id") -> str:
try:
@@ -32,14 +33,20 @@ def validate_uuid(value: str, field_name: str = "id") -> str:
async def create_message(
chat_id: str,
body: CreateMessageRequest,
idempotency_key: str | None = Header(default=None, alias="Idempotency-Key"),
current_user: UserResponse = Depends(get_current_user),
) -> MessageResponse:
"""Отправка сообщения в чат."""
if not idempotency_key or not IDEMPOTENCY_KEY_REGEX.match(idempotency_key):
raise InvalidRequestError(
"Заголовок 'Idempotency-Key' обязателен и должен соответствовать шаблону ^[A-Za-z0-9._:-]{1,128}$"
)
valid_id = validate_uuid(chat_id, "chat_id")
msg = await storage.send_message(
caller_id=current_user.id,
chat_id=valid_id,
text=body.text,
idempotency_key=idempotency_key,
)
return msg