385 lines
14 KiB
Python
385 lines
14 KiB
Python
import base64
|
|
import binascii
|
|
import json
|
|
import logging
|
|
import os
|
|
import re
|
|
import time
|
|
from contextlib import asynccontextmanager
|
|
from threading import BoundedSemaphore
|
|
from uuid import uuid4
|
|
|
|
from fastapi import Depends, FastAPI, Request, Response
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from opentelemetry import propagate
|
|
from opentelemetry.trace import SpanKind, Status, StatusCode
|
|
from starlette.concurrency import run_in_threadpool
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from . import telemetry
|
|
from .attachments import AttachmentService
|
|
from .domain import Failure, boundary, identifier, page
|
|
from .models import CreateChat, CreateMessage, CreateUser
|
|
from .postgres import PostgresStore
|
|
from .secrets_provider import refresh
|
|
from .storage import ObjectStorage
|
|
|
|
logger = logging.getLogger("messenger")
|
|
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"), format="%(message)s")
|
|
INSTANCE_ID = telemetry.INSTANCE_ID
|
|
|
|
CODES = {
|
|
400: "invalid_request",
|
|
401: "unauthorized",
|
|
404: "not_found",
|
|
409: "conflict",
|
|
413: "payload_too_large",
|
|
415: "unsupported_media_type",
|
|
429: "rate_limited",
|
|
503: "unavailable",
|
|
500: "internal_error",
|
|
}
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app):
|
|
app.state.telemetry = telemetry.setup("messenger-api")
|
|
app.state.store = PostgresStore()
|
|
app.state.telemetry.start_background(app.state.store)
|
|
app.state.storage = ObjectStorage()
|
|
app.state.attachments = AttachmentService(app.state.store, app.state.storage)
|
|
app.state.upload_slots = BoundedSemaphore(1)
|
|
logger.info(json.dumps({"event": "started", "storage": "postgres"}))
|
|
yield
|
|
logger.info(json.dumps({"event": "stopped"}))
|
|
app.state.telemetry.close()
|
|
app.state.storage.close()
|
|
app.state.store.close()
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
|
|
|
|
|
|
def error(request, status, message, headers=None):
|
|
return JSONResponse(
|
|
{
|
|
"error": {
|
|
"code": CODES.get(status, "invalid_request"),
|
|
"message": message,
|
|
"request_id": request.state.request_id,
|
|
}
|
|
},
|
|
status_code=status,
|
|
headers=headers,
|
|
)
|
|
|
|
|
|
async def invalid_json_encoding(request: Request):
|
|
path = request.url.path
|
|
if request.method != "POST" or not (
|
|
path in {"/api/v1/users", "/api/v1/chats"}
|
|
or re.fullmatch(r"/api/v1/chats/[^/]+/messages", path)
|
|
):
|
|
return False
|
|
raw = await request.body()
|
|
try:
|
|
raw.decode("utf-8")
|
|
except UnicodeDecodeError:
|
|
return True
|
|
# Literal NUL is invalid JSON; it also prevents json.loads(bytes) from
|
|
# autodetecting BOM-less UTF-16/32. Escaped JSON \u0000 remains valid.
|
|
return b"\x00" in raw
|
|
|
|
|
|
@app.middleware("http")
|
|
async def request_log(request: Request, call_next):
|
|
request.state.request_id = str(uuid4())
|
|
start = time.perf_counter()
|
|
observed = not request.url.path.startswith("/health/")
|
|
if not observed:
|
|
try:
|
|
if request.url.path != "/health/live":
|
|
await run_in_threadpool(refresh)
|
|
response = await call_next(request)
|
|
except Failure as exc:
|
|
response = error(request, exc.status, exc.message, exc.headers)
|
|
response.headers["X-Request-Id"] = request.state.request_id
|
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
|
return response
|
|
method = (
|
|
request.method
|
|
if request.method in {"GET", "POST", "DELETE", "PUT", "PATCH", "HEAD", "OPTIONS"}
|
|
else "OTHER"
|
|
)
|
|
context = propagate.extract(dict(request.headers))
|
|
with telemetry.operation("HTTP " + method, context=context, kind=SpanKind.SERVER) as span:
|
|
try:
|
|
await run_in_threadpool(refresh)
|
|
if (
|
|
request.method == "POST"
|
|
and request.url.path == "/api/v1/auth/sessions"
|
|
and await request.body()
|
|
):
|
|
response = error(request, 400, "Вход не принимает тело запроса")
|
|
elif await invalid_json_encoding(request):
|
|
response = error(request, 400, "JSON должен быть закодирован в UTF-8")
|
|
else:
|
|
response = await call_next(request)
|
|
except Failure as exc:
|
|
response = error(request, exc.status, exc.message, exc.headers)
|
|
except Exception:
|
|
logger.error(
|
|
json.dumps({"event": "unexpected_error", "request_id": request.state.request_id})
|
|
)
|
|
response = error(request, 500, "Внутренняя ошибка")
|
|
response.headers["X-Request-Id"] = request.state.request_id
|
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
|
response.headers["X-Trace-Id"] = format(span.get_span_context().trace_id, "032x")
|
|
response.headers["X-Trace-Sampled"] = "true" if span.is_recording() else "false"
|
|
route = getattr(request.scope.get("route"), "path", "unknown")
|
|
elapsed = time.perf_counter() - start
|
|
attrs = {
|
|
"method": method,
|
|
"route": route,
|
|
"status_class": str(response.status_code // 100) + "xx",
|
|
}
|
|
span.update_name(method + " " + route)
|
|
span.set_attributes(
|
|
{
|
|
"http.request.method": method,
|
|
"http.route": route,
|
|
"http.response.status_code": response.status_code,
|
|
}
|
|
)
|
|
if response.status_code >= 500:
|
|
span.set_status(Status(StatusCode.ERROR, "server_error"))
|
|
t = telemetry.current()
|
|
if observed and t:
|
|
t.requests.add(1, attrs)
|
|
t.http_duration.record(elapsed, attrs)
|
|
t.sampling.add(1, {"decision": "sampled" if span.is_recording() else "not_sampled"})
|
|
logger.info(
|
|
json.dumps(
|
|
{
|
|
"event": "request",
|
|
"request_id": request.state.request_id,
|
|
**attrs,
|
|
"status": response.status_code,
|
|
"duration_ms": round(elapsed * 1000, 3),
|
|
}
|
|
)
|
|
)
|
|
return response
|
|
|
|
|
|
@app.exception_handler(Failure)
|
|
async def domain_error(request, exc):
|
|
return error(request, exc.status, exc.message, exc.headers)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_error(request, exc):
|
|
return error(request, 400, "Неверный JSON или поля запроса")
|
|
|
|
|
|
@app.exception_handler(HTTPException)
|
|
async def http_error(request, exc):
|
|
return error(request, exc.status_code, "Неверный запрос", exc.headers)
|
|
|
|
|
|
def bearer_failure():
|
|
return Failure(
|
|
401, "Требуется действующая сессия", {"WWW-Authenticate": 'Bearer realm="messenger"'}
|
|
)
|
|
|
|
|
|
def bearer(request):
|
|
parts = request.headers.get("Authorization", "").split()
|
|
if len(parts) != 2 or parts[0].lower() != "bearer":
|
|
raise bearer_failure()
|
|
return parts[1]
|
|
|
|
|
|
def actor(request: Request):
|
|
user = request.app.state.store.authenticate(bearer(request))
|
|
if user is None:
|
|
raise bearer_failure()
|
|
return user
|
|
|
|
|
|
@app.post("/api/v1/auth/sessions", status_code=201)
|
|
def login(request: Request):
|
|
headers = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
|
|
parts = request.headers.get("Authorization", "").split()
|
|
try:
|
|
if len(parts) != 2 or parts[0].lower() != "basic" or len(parts[1]) > 2048:
|
|
raise ValueError
|
|
decoded = base64.b64decode(parts[1], validate=True).decode("utf-8")
|
|
username, password = decoded.split(":", 1)
|
|
if not re.fullmatch(r"[a-z0-9_]{3,32}", username) or not 12 <= len(password) <= 128:
|
|
raise ValueError
|
|
except (ValueError, UnicodeError, binascii.Error):
|
|
raise Failure(401, "Неверные учётные данные", headers) from None
|
|
session = request.app.state.store.login(username, password)
|
|
if session is None:
|
|
raise Failure(401, "Неверные учётные данные", headers)
|
|
return session
|
|
|
|
|
|
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
|
def logout(request: Request, user=Depends(actor)):
|
|
if not request.app.state.store.logout(bearer(request)):
|
|
raise bearer_failure()
|
|
return Response(status_code=204)
|
|
|
|
|
|
def pagination(request, scope):
|
|
allowed = {"limit", "cursor"}
|
|
if any(k not in allowed for k in request.query_params):
|
|
raise Failure(400, "Неизвестный параметр запроса")
|
|
if any(len(request.query_params.getlist(k)) > 1 for k in allowed):
|
|
raise Failure(400, "Повторный параметр запроса")
|
|
value = request.query_params.get("limit", "50")
|
|
if len(value) > 3 or not re.fullmatch(r"[0-9]+", value) or not 1 <= int(value) <= 100:
|
|
raise Failure(400, "limit должен быть целым от 1 до 100")
|
|
return int(value), boundary(scope, request.query_params.get("cursor"))
|
|
|
|
|
|
@app.get("/health/live")
|
|
def live():
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.get("/health/ready")
|
|
def ready(request: Request):
|
|
if not request.app.state.store.ready() or not request.app.state.storage.ready():
|
|
raise Failure(503, "Хранилище недоступно")
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.post("/api/v1/users", status_code=201)
|
|
def register(data: CreateUser, request: Request):
|
|
return request.app.state.store.register(data.model_dump(mode="json"))
|
|
|
|
|
|
@app.get("/api/v1/users/me")
|
|
def me(user=Depends(actor)):
|
|
return user
|
|
|
|
|
|
@app.post("/api/v1/chats", status_code=201)
|
|
def create_chat(data: CreateChat, request: Request, user=Depends(actor)):
|
|
return request.app.state.store.create_chat(user["id"], data.model_dump(mode="json"))
|
|
|
|
|
|
@app.get("/api/v1/chats")
|
|
def chats(request: Request, user=Depends(actor)):
|
|
scope = "chats:" + user["id"]
|
|
limit, after = pagination(request, scope)
|
|
return page(request.app.state.store.list_chats(user["id"], after, limit), limit, scope)
|
|
|
|
|
|
@app.get("/api/v1/chats/{chat_id}")
|
|
def chat(chat_id: str, request: Request, user=Depends(actor)):
|
|
return request.app.state.store.chat(user["id"], identifier(chat_id))
|
|
|
|
|
|
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
|
def send(chat_id: str, data: CreateMessage, request: Request, user=Depends(actor)):
|
|
key = request.headers.get("Idempotency-Key", "")
|
|
if not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key):
|
|
raise Failure(400, "Требуется корректный Idempotency-Key")
|
|
return request.app.state.store.send(
|
|
user["id"], identifier(chat_id), data.text, key, [str(a) for a in data.attachment_ids]
|
|
)
|
|
|
|
|
|
@app.get("/api/v1/chats/{chat_id}/messages")
|
|
def history(chat_id: str, request: Request, user=Depends(actor)):
|
|
cid = identifier(chat_id)
|
|
scope = "messages:" + cid
|
|
limit, after = pagination(request, scope)
|
|
return page(request.app.state.store.history(user["id"], cid, after, limit), limit, scope)
|
|
|
|
|
|
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=202)
|
|
async def upload_attachment(chat_id: str, request: Request, user=Depends(actor)):
|
|
import asyncio
|
|
import tempfile
|
|
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from .attachments import MAX_BYTES, filename_valid
|
|
|
|
if (
|
|
set(request.query_params) != {"filename"}
|
|
or len(request.query_params.getlist("filename")) != 1
|
|
):
|
|
raise Failure(400, "Требуется одно имя файла")
|
|
filename = filename_valid(request.query_params["filename"])
|
|
cid = identifier(chat_id)
|
|
await run_in_threadpool(request.app.state.store.chat, user["id"], cid)
|
|
content_type = request.headers.get("content-type", "").lower()
|
|
if content_type not in {"image/png", "image/jpeg"}:
|
|
raise Failure(415, "Ожидается PNG или JPEG")
|
|
if not request.app.state.upload_slots.acquire(blocking=False):
|
|
raise Failure(429, "Все места загрузки заняты", {"Retry-After": "1"})
|
|
try:
|
|
with tempfile.SpooledTemporaryFile(max_size=256 * 1024) as spool:
|
|
size = 0
|
|
try:
|
|
async with asyncio.timeout(15):
|
|
async for chunk in request.stream():
|
|
size += len(chunk)
|
|
if size > MAX_BYTES:
|
|
raise Failure(413, "Файл больше 10 MiB")
|
|
spool.write(chunk)
|
|
except TimeoutError:
|
|
raise Failure(400, "Истекло время загрузки") from None
|
|
spool.seek(0)
|
|
return await run_in_threadpool(
|
|
request.app.state.attachments.upload,
|
|
user["id"],
|
|
cid,
|
|
filename,
|
|
content_type,
|
|
spool,
|
|
size,
|
|
)
|
|
finally:
|
|
request.app.state.upload_slots.release()
|
|
|
|
|
|
@app.get("/api/v1/attachments/{attachment_id}")
|
|
def attachment_metadata(attachment_id: str, request: Request, user=Depends(actor)):
|
|
return request.app.state.attachments.metadata(user["id"], identifier(attachment_id))
|
|
|
|
|
|
@app.get("/api/v1/attachments/{attachment_id}/content")
|
|
def attachment_content(attachment_id: str, request: Request, user=Depends(actor)):
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
if (
|
|
any(k != "variant" for k in request.query_params)
|
|
or len(request.query_params.getlist("variant")) > 1
|
|
):
|
|
raise Failure(400, "Неверный вариант")
|
|
variant = request.query_params.get("variant", "original")
|
|
if variant not in {"original", "thumbnail", "crop", "optimized"}:
|
|
raise Failure(400, "Неверный вариант")
|
|
obj = request.app.state.attachments.content(user["id"], identifier(attachment_id), variant)
|
|
|
|
def chunks():
|
|
try:
|
|
while data := obj["Body"].read(64 * 1024):
|
|
yield data
|
|
finally:
|
|
obj["Body"].close()
|
|
|
|
return StreamingResponse(
|
|
chunks(),
|
|
media_type=obj["ContentType"],
|
|
headers={"Content-Length": str(obj["ContentLength"])},
|
|
)
|