feat: добавить API, compose, nginx и отчёт для лабораторной 3

Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
Evgeny Bushkov
2026-11-06 00:22:31 +03:00
co-authored by aider
parent d8ba046033
commit 158dc10344
26 changed files with 1110 additions and 13 deletions
+46
View File
@@ -0,0 +1,46 @@
import uuid
from datetime import datetime, timezone
from fastapi import Depends, Request
from sqlalchemy import select
from sqlalchemy.orm import Session
from .db import SessionLocal
from .errors import ApiError
from .models import SessionRow, User
from .security import token_hash
def get_db():
s = SessionLocal()
try:
yield s
finally:
s.close()
def current_user(request: Request, db: Session = Depends(get_db)) -> User:
auth = request.headers.get("authorization", "")
if not auth.lower().startswith("bearer "):
raise ApiError(401, "unauthorized", "Требуется Bearer-токен")
token = auth[7:].strip()
if not token:
raise ApiError(401, "unauthorized", "Пустой токен")
row = db.get(SessionRow, token_hash(token))
if row is None:
raise ApiError(401, "unauthorized", "Неизвестный токен")
if row.expires_at <= datetime.now(timezone.utc):
raise ApiError(401, "unauthorized", "Сессия истекла")
user = db.get(User, row.user_id)
if user is None:
raise ApiError(401, "unauthorized", "Пользователь не найден")
request.state.user = user
request.state.token_hash = row.token_hash
return user
def parse_uuid(value: str) -> uuid.UUID:
try:
return uuid.UUID(value)
except (ValueError, TypeError):
raise ApiError(400, "invalid_request", "Некорректный UUID")