forked from mtusi2026/mtusi2026lab3
feat-(lab3 started, X-Instance-ID, Idempotence-Key in send_message method added)
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
FROM python:3.13-slim AS Builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY requirements.txt .
|
||||
|
||||
RUN python -m venv /opt/venv
|
||||
|
||||
ENV PATH="/opt/venv/bin:$PATH"
|
||||
|
||||
RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
FROM python:3.13-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=Builder /opt/venv /opt/venv
|
||||
|
||||
ENV PATH="/opt/venv/bin:$PATH"
|
||||
|
||||
RUN groupadd -r appuser && useradd -r -g appuser -d /app -s /sbin/nologin appuser
|
||||
|
||||
COPY src/ ./src/
|
||||
|
||||
COPY scripts/ ./scripts/
|
||||
|
||||
RUN chown -R appuser:appuser /app
|
||||
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/live')" || exit 1
|
||||
|
||||
STOPSIGNAL SIGTERM
|
||||
|
||||
CMD ["python", "-m", "uvicorn", "src.main:application", "--host", "0.0.0.0", "--port", "8080", "--workers", "1"]
|
||||
@@ -1,4 +1,5 @@
|
||||
PYTHON ?= python3
|
||||
PYTHON ?= python
|
||||
export PYTHONUTF8 := 1
|
||||
BASE_URL ?= http://localhost:8080
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
|
||||
@@ -1,18 +1,29 @@
|
||||
# Отчёт — лабораторная 3
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Автор, группа: Синюк Максим Павлович БПИ2301
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Использованные источники, библиотеки и помощь ИИ: перенос базы lab2, python, fastapi, uvicorn, pydantic, pytest, sqlalchemy, argon2-cffi, redis
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
|
||||
git clone -b "solution/БПИ2301-Синюк" https://gitea.privcloud.tech/Atelier77/mtusi2026lab3
|
||||
docker compose build
|
||||
docker compose up -d
|
||||
docker ps
|
||||
docker compose down
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
|
||||
Из lab2 перенесены описание классов для работы с таблицами БД через ORM - src/api/repositories/
|
||||
модели ORM таблиц для PostgreSQL - src/models/
|
||||
валидация - src/pydantic_valid/
|
||||
student_tests/
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
services:
|
||||
nginx:
|
||||
image: nginx:1.25-alpine
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition:
|
||||
service_healthy
|
||||
restart: unless-stopped
|
||||
api:
|
||||
build: .
|
||||
environment:
|
||||
- APP_HOST=${APP_HOST}
|
||||
- APP_PORT=${APP_PORT}
|
||||
- DATABASE_URL=${DATABASE_URL}
|
||||
- SESSION_TTL_SECONDS=${SESSION_TTL_SECONDS}
|
||||
- REDIS_URL=${REDIS_URL}
|
||||
- INSTANCE_ID_PREFIX=api
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)"] #засекретить
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
start_period: 15s
|
||||
retries: 3
|
||||
deploy:
|
||||
replicas: 2
|
||||
|
||||
postgres:
|
||||
image: postgres:16.3-alpine
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
ports:
|
||||
- "127.0.0.1:5433:5432"
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
- ./migrations:/docker-entrypoint-initdb.d:ro
|
||||
command: postgres -c shared_preload_libraries=pg_stat_statements
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 5
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:8.10-alpine
|
||||
command: redis-server --appendonly yes --save 60 1
|
||||
ports:
|
||||
- "127.0.0.1:6379:6379"
|
||||
volumes:
|
||||
- redisdata:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 5s
|
||||
retries: 5
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
redisdata:
|
||||
@@ -0,0 +1,31 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
upstream backend{
|
||||
server api:8080 max_fails=2 fail_timeout=10s;
|
||||
}
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_read_timeout 30s;
|
||||
|
||||
location / {
|
||||
proxy_pass http://backend;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location /nginx-health {
|
||||
access_log off;
|
||||
return 200 "healthy\n";
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,88 @@
|
||||
from fastapi import Request, HTTPException, Depends, status
|
||||
from typing import Annotated
|
||||
from src.api.repositories.main_repo import Repository
|
||||
from src.models.users import User
|
||||
from src.auth.tokens import hash_token
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
import asyncio
|
||||
from src.redis_client import get_redis
|
||||
from src.database import get_db
|
||||
from src.services.session_cache import invalidate_session, get_cached_user_id, cache_session
|
||||
|
||||
|
||||
type DbSession = Annotated[AsyncSession, Depends(get_db)] ##Dependency Injection
|
||||
|
||||
_session_refresh_locks: dict[str, asyncio.Lock] = {}
|
||||
|
||||
|
||||
async def get_current_user(request: Request, session: DbSession) -> User:
|
||||
repo = Repository(session)
|
||||
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if not auth_header or not auth_header.startswith("Bearer "):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="missing or invalid Bearer token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'},
|
||||
)
|
||||
|
||||
raw_token = auth_header[7:]
|
||||
token_hash = hash_token(raw_token)
|
||||
|
||||
redis_is_alive = get_redis() is not None
|
||||
|
||||
if redis_is_alive:
|
||||
|
||||
cached_user_id = await get_cached_user_id(token_hash)
|
||||
if cached_user_id is not None:
|
||||
user = await repo.users.get_by_id(cached_user_id)
|
||||
if user:
|
||||
return user
|
||||
await invalidate_session(token_hash)
|
||||
|
||||
lock = _session_refresh_locks.setdefault(token_hash, asyncio.Lock())
|
||||
|
||||
async with lock:
|
||||
cached_user_id = await get_cached_user_id(token_hash)
|
||||
if cached_user_id is not None:
|
||||
user = await repo.users.get_by_id(cached_user_id)
|
||||
if user:
|
||||
return user
|
||||
|
||||
|
||||
current_session = await repo.sessions.get_by_token_hash(token_hash)
|
||||
if not current_session:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid or expired token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
user = await repo.users.get_by_id(current_session.user_id)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="user not found",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
await cache_session(token_hash, user.id)
|
||||
return user
|
||||
|
||||
else:
|
||||
current_session = await repo.sessions.get_by_token_hash(token_hash)
|
||||
if not current_session:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid or expired token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
user = await repo.users.get_by_id(current_session.user_id)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="user not found",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
return user
|
||||
|
||||
|
||||
type CurrentUser = Annotated[User, Depends(get_current_user)]
|
||||
@@ -0,0 +1,96 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Optional, List, Tuple
|
||||
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from src.models.chats import Chat, ChatMember
|
||||
from src.api.repositories.core import DatabaseManager
|
||||
|
||||
|
||||
class ChatRepo(DatabaseManager):
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(session, Chat)
|
||||
|
||||
|
||||
async def create_with_members(self, title: str, member_ids: List[uuid.UUID]) -> Optional[Chat]:
|
||||
try:
|
||||
chat = Chat(title=title)
|
||||
self.session.add(chat)
|
||||
await self.session.flush()
|
||||
|
||||
for user_id in member_ids:
|
||||
member = ChatMember(chat_id=chat.id, user_id=user_id)
|
||||
self.session.add(member)
|
||||
|
||||
await self.session.commit()
|
||||
|
||||
await self.session.refresh(chat)
|
||||
|
||||
result = await self.session.execute(select(Chat).options(selectinload(Chat.members)).where(Chat.id == chat.id))
|
||||
return result.scalar_one()
|
||||
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
async def get_by_id_with_members(self, chat_id: uuid.UUID) -> Optional[Chat]:
|
||||
result = await self.session.execute(select(Chat).options(selectinload(Chat.members)).where(Chat.id == chat_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_user_chats(self, user_id: uuid.UUID) -> List[Chat]:
|
||||
result = await self.session.execute(select(Chat).join(ChatMember, Chat.id == ChatMember.chat_id).options(selectinload(Chat.members)).where(ChatMember.user_id == user_id).order_by(Chat.created_at.asc(), Chat.id.asc()))
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def is_user_member(self, chat_id: uuid.UUID, user_id: uuid.UUID) -> bool:
|
||||
result = await self.session.execute(select(ChatMember).where(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id,))
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
|
||||
async def delete_by_id(self, chat_id: uuid.UUID) -> bool:
|
||||
try:
|
||||
chat = await self.get_by_id_with_members(chat_id)
|
||||
if chat:
|
||||
await self.session.delete(chat)
|
||||
await self.session.commit()
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
async def get_user_chats_paginated(
|
||||
self,
|
||||
user_id: uuid.UUID,
|
||||
limit: int = 50,
|
||||
cursor_created_at: Optional[datetime] = None,
|
||||
cursor_chat_id: Optional[uuid.UUID] = None,
|
||||
) -> Tuple[List[Chat], bool]:
|
||||
|
||||
query = (select(Chat).join(ChatMember, Chat.id == ChatMember.chat_id).options(selectinload(Chat.members)).where(ChatMember.user_id == user_id).order_by(Chat.created_at.asc(), Chat.id.asc()))
|
||||
|
||||
if cursor_created_at is not None and cursor_chat_id is not None:
|
||||
query = query.where(
|
||||
(Chat.created_at > cursor_created_at) |
|
||||
(
|
||||
(Chat.created_at == cursor_created_at) &
|
||||
(Chat.id > cursor_chat_id)
|
||||
)
|
||||
)
|
||||
|
||||
query = query.limit(limit + 1)
|
||||
|
||||
result = await self.session.execute(query)
|
||||
chats = list(result.scalars().all())
|
||||
|
||||
has_more = len(chats) > limit
|
||||
if has_more:
|
||||
chats = chats[:limit]
|
||||
|
||||
return chats, has_more
|
||||
@@ -0,0 +1,130 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.engine import Result
|
||||
from typing import List, Optional, Any, Type, TypeVar
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
ModelType = TypeVar("ModelType")
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
def __init__(self, session: AsyncSession, model: Type[ModelType]):
|
||||
self.session = session
|
||||
self.model = model
|
||||
|
||||
async def execute_query(self, query) -> Result:
|
||||
return await self.session.execute(query)
|
||||
|
||||
async def get_all(self) -> List[ModelType]:
|
||||
result = await self.session.execute(select(self.model))
|
||||
return result.scalars().all()
|
||||
|
||||
async def get_by_id(self, record_id: uuid.UUID) -> Optional[ModelType]:
|
||||
result = await self.session.execute(select(self.model).where(self.model.id == record_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def create(self, **data) -> ModelType:
|
||||
try:
|
||||
instance = self.model(**data)
|
||||
self.session.add(instance)
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error creating record in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
try:
|
||||
await self.session.commit()
|
||||
await self.session.refresh(instance)
|
||||
return instance
|
||||
except IntegrityError:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error: field value already exist")
|
||||
raise
|
||||
|
||||
async def bulk_create(self, data_list: List[dict]) -> List[ModelType]:
|
||||
try:
|
||||
instances = [self.model(**data) for data in data_list]
|
||||
self.session.add_all(instances)
|
||||
await self.session.commit()
|
||||
|
||||
for instance in instances:
|
||||
await self.session.refresh(instance)
|
||||
|
||||
return instances
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error bulk creating records in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def update_by_id(self, record_id: uuid.UUID, **data) -> Optional[ModelType]:
|
||||
try:
|
||||
instance = await self.get_by_id(self.model, record_id)
|
||||
if instance:
|
||||
for key, value in data.items():
|
||||
setattr(instance, key, value)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(instance)
|
||||
return instance
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error updating record in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def delete_by_id(self, record_id: uuid.UUID) -> bool:
|
||||
try:
|
||||
instance = await self.get_by_id(self.model, record_id)
|
||||
if instance:
|
||||
await self.session.delete(instance)
|
||||
await self.session.commit()
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error deleting record in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def get_by_field(self, field_name: str, value: Any) -> Optional[ModelType]:
|
||||
result = await self.session.execute(select(self.model).where(getattr(self.model, field_name) == value))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def delete_by_field(self, field_name: str, value: Any) -> bool:
|
||||
try:
|
||||
instance = await self.get_by_field(field_name, value)
|
||||
if instance:
|
||||
await self.session.delete(instance)
|
||||
await self.session.commit()
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
await self.session.rollback()
|
||||
logger.error(f"Error deleting record in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def update_by_field(self, field_name: str, value: Any, **data) -> Optional[ModelType]:
|
||||
try:
|
||||
instance = await self.get_by_field(field_name, value)
|
||||
if instance:
|
||||
for key, value in data.items():
|
||||
setattr(instance, key, value)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(instance)
|
||||
return instance
|
||||
except Exception as e:
|
||||
self.session.rollback()
|
||||
logger.error(f"Error in updating record in {self.model.__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def get_many_by_field(self, field_name: str, value: Any) -> List[ModelType]:
|
||||
result = await self.session.execute(select(self.model).where(getattr(self.model, field_name) == value))
|
||||
return result.scalars().all()
|
||||
|
||||
async def exists(self, record_id: int) -> bool:
|
||||
instance = await self.get_by_id(self.model, record_id)
|
||||
return instance is not None
|
||||
|
||||
async def count(self) -> int:
|
||||
result = await self.session.execute(select(self.model))
|
||||
return len(result.scalars().all())
|
||||
@@ -0,0 +1,13 @@
|
||||
from src.api.repositories.user_repo import UserRepo
|
||||
from src.api.repositories.session_repo import SessionRepo
|
||||
from src.api.repositories.chat_repo import ChatRepo
|
||||
from src.api.repositories.message_repo import MessageRepo
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
|
||||
class Repository():
|
||||
def __init__(self, session: AsyncSession):
|
||||
self.users = UserRepo(session)
|
||||
self.sessions = SessionRepo(session)
|
||||
self.chats = ChatRepo(session)
|
||||
self.messages = MessageRepo(session)
|
||||
@@ -0,0 +1,37 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Optional, List, Tuple
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.models.messages import Message
|
||||
from src.api.repositories.core import DatabaseManager
|
||||
|
||||
|
||||
class MessageRepo(DatabaseManager):
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(session, Message)
|
||||
|
||||
async def create(self, chat_id: uuid.UUID, sender_id: uuid.UUID, text: str) -> Message:
|
||||
return await super().create(chat_id=chat_id, sender_id=sender_id, text=text)
|
||||
|
||||
async def get_chat_messages(self, chat_id: uuid.UUID, limit: int = 50, cursor_created_at: Optional[datetime] = None, cursor_msg_id: Optional[uuid.UUID] = None) -> Tuple[List[Message], bool]:
|
||||
query = (select(Message).where(Message.chat_id == chat_id).order_by(Message.created_at.asc(), Message.id.asc())
|
||||
)
|
||||
|
||||
if cursor_created_at is not None and cursor_msg_id is not None:
|
||||
query = query.where((Message.created_at > cursor_created_at) |
|
||||
((Message.created_at == cursor_created_at) & (Message.id > cursor_msg_id))
|
||||
)
|
||||
|
||||
query = query.limit(limit + 1)
|
||||
|
||||
result = await self.session.execute(query)
|
||||
messages = list(result.scalars().all())
|
||||
|
||||
has_more = len(messages) > limit
|
||||
if has_more:
|
||||
messages = messages[:limit]
|
||||
|
||||
return messages, has_more
|
||||
@@ -0,0 +1,32 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from src.models.sessions import Session
|
||||
from src.api.repositories.core import DatabaseManager
|
||||
from src.models.sessions import Session
|
||||
|
||||
|
||||
class SessionRepo(DatabaseManager):
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(session, Session)
|
||||
|
||||
async def create(self, token_hash: str, user_id: uuid.UUID, expires_at: datetime) -> Optional[Session]:
|
||||
result = await super().create(token_hash=token_hash, user_id=user_id, expires_at=expires_at)
|
||||
return result
|
||||
|
||||
async def get_by_token_hash(self, token_hash: str) -> Optional[Session]:
|
||||
result = await super().get_by_field("token_hash", token_hash)
|
||||
return result
|
||||
|
||||
async def get_by_user_id(self, user_id: uuid.UUID) -> Optional[Session]:
|
||||
result = await super().get_by_field("user_id", user_id)
|
||||
return result
|
||||
|
||||
async def delete_by_user_id(self, user_id: uuid.UUID) -> bool:
|
||||
result = await super().delete_by_field("user_id", user_id)
|
||||
return result
|
||||
|
||||
async def delete_by_token_hash(self, token_hash: str) -> bool:
|
||||
result = await super().delete_by_field("token_hash", token_hash)
|
||||
return result
|
||||
@@ -0,0 +1,29 @@
|
||||
import uuid
|
||||
from typing import Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from src.models.users import User
|
||||
from src.api.repositories.core import DatabaseManager
|
||||
from src.utils import generate_id
|
||||
|
||||
|
||||
class UserRepo(DatabaseManager):
|
||||
def __init__(self, session: AsyncSession):
|
||||
super().__init__(session, User)
|
||||
|
||||
async def get_by_username(self, username: str) -> Optional[User]:
|
||||
result = await super().get_by_field("username", username.lower())
|
||||
return result
|
||||
|
||||
async def get_by_id(self, user_id: uuid.UUID) -> Optional[User]:
|
||||
result = await super().get_by_id(user_id)
|
||||
return result
|
||||
|
||||
async def create(self, username: str, display_name: str, password_hash: str) -> Optional[User]:
|
||||
try:
|
||||
result = await super().create(id=generate_id(), username=username.lower(), display_name=display_name, password_hash=password_hash)
|
||||
return result
|
||||
|
||||
except IntegrityError:
|
||||
return None
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import base64
|
||||
from fastapi import APIRouter, HTTPException, Request, Response, status
|
||||
from src.api.dependencies import DbSession, CurrentUser
|
||||
from src.pydantic_valid.session import SessionUser, SessionCreateResponse
|
||||
from src.api.repositories.main_repo import Repository
|
||||
from src.auth.password import verify_password
|
||||
from src.auth.tokens import generate_token, hash_token, calculate_expires_at
|
||||
from src.config import settings
|
||||
from src.services.session_cache import cache_session, invalidate_session
|
||||
|
||||
router = APIRouter(prefix="/v1/auth")
|
||||
|
||||
|
||||
@router.post("/sessions", response_model=SessionCreateResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_session(request: Request, session: DbSession):
|
||||
repo = Repository(session)
|
||||
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if not auth_header or not auth_header.startswith("Basic "):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="missing or invalid Basic auth header",
|
||||
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
try:
|
||||
encoded_credentials = auth_header[6:]
|
||||
decoded = base64.b64decode(encoded_credentials).decode("utf-8")
|
||||
username, password = decoded.split(":", 1)
|
||||
except Exception:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid credentials format",
|
||||
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
|
||||
user = await repo.users.get_by_username(username)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid username or password",
|
||||
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
|
||||
if not verify_password(password, user.password_hash):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid username or password",
|
||||
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
|
||||
)
|
||||
|
||||
raw_token, token_hash = generate_token()
|
||||
expires_at = calculate_expires_at(settings.SESSION_TTL_SECONDS)
|
||||
|
||||
await repo.sessions.create(token_hash=token_hash, user_id=user.id, expires_at=expires_at)
|
||||
await cache_session(token_hash, user.id)
|
||||
|
||||
return SessionCreateResponse(
|
||||
token=raw_token,
|
||||
expires_at=expires_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z",
|
||||
user=SessionUser(
|
||||
id=str(user.id),
|
||||
username=user.username,
|
||||
display_name=user.display_name,
|
||||
created_at=user.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/sessions/current", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_current_session(request: Request, session: DbSession, current_user: CurrentUser):
|
||||
repo = Repository(session)
|
||||
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if not auth_header or not auth_header.startswith("Bearer "):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid or missing Bearer token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
raw_token = auth_header[7:]
|
||||
token_hash = hash_token(raw_token)
|
||||
|
||||
user_session = await repo.sessions.get_by_token_hash(token_hash)
|
||||
if not user_session:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
if user_session.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="token in request doesn't align with current_user",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
await invalidate_session(token_hash)
|
||||
deleted = await repo.sessions.delete_by_token_hash(token_hash)
|
||||
if not deleted:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="invalid or expired token",
|
||||
headers={"WWW-Authenticate": 'Bearer realm="messenger"'})
|
||||
|
||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||
@@ -0,0 +1,113 @@
|
||||
from fastapi import HTTPException, APIRouter, Query
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
from datetime import datetime
|
||||
from src.pydantic_valid.chat import CreateChat, Chat
|
||||
from src.pydantic_valid.page_response import PageResponse
|
||||
from src.api.dependencies import CurrentUser, DbSession
|
||||
from src.api.repositories.main_repo import Repository
|
||||
from src.utils import generate_id, get_now_iso
|
||||
import base64
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _encode_chat_cursor(created_at: str, chat_id: str) -> str:
|
||||
raw = f"c|{created_at}|{chat_id}"
|
||||
return base64.urlsafe_b64encode(raw.encode("utf-8")).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def _decode_chat_cursor(cursor: str) -> tuple[str, str]:
|
||||
try:
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded).decode("utf-8")
|
||||
parts = raw.split("|", 2)
|
||||
if len(parts) != 3 or parts[0] != "c":
|
||||
raise ValueError("bad cursor structure")
|
||||
created_at, chat_id = parts[1], parts[2]
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail="invalid cursor") from e
|
||||
|
||||
return created_at, chat_id
|
||||
|
||||
|
||||
@router.post("/v1/chats", response_model=Chat, status_code=201)
|
||||
async def create_chat(session: DbSession, current_user: CurrentUser, chat_in: CreateChat) -> Chat:
|
||||
repo = Repository(session)
|
||||
|
||||
if str(current_user.id) not in chat_in.member_ids:
|
||||
raise HTTPException(status_code=400, detail="Creator of chat should be in chat member list")
|
||||
|
||||
member_uuids = [UUID(mid) for mid in chat_in.member_ids]
|
||||
|
||||
for member_id in member_uuids:
|
||||
user = await repo.users.get_by_id(member_id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=400, detail="Unexisting user_id in chat member list")
|
||||
|
||||
chat = await repo.chats.create_with_members(title=chat_in.title, member_ids=member_uuids)
|
||||
|
||||
return Chat(
|
||||
id=str(chat.id),
|
||||
title=chat.title,
|
||||
member_ids=[str(m.user_id) for m in chat.members],
|
||||
created_at = chat.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/v1/chats", response_model=PageResponse)
|
||||
async def list_chats(current_user: CurrentUser, session: DbSession, limit: int = Query(default=50, ge=1, le=100), cursor: Optional[str] = Query(default=None, min_length=1)) -> PageResponse:
|
||||
repo = Repository(session)
|
||||
|
||||
cursor_created_at = None
|
||||
cursor_chat_id = None
|
||||
if cursor:
|
||||
created_at_str, chat_id_str = _decode_chat_cursor(cursor)
|
||||
cursor_created_at = datetime.fromisoformat(created_at_str)
|
||||
cursor_chat_id = UUID(chat_id_str)
|
||||
|
||||
chats, has_more = await repo.chats.get_user_chats_paginated(
|
||||
user_id=current_user.id,
|
||||
limit=limit,
|
||||
cursor_created_at=cursor_created_at,
|
||||
cursor_chat_id=cursor_chat_id,
|
||||
)
|
||||
|
||||
next_cursor = None
|
||||
if has_more and chats:
|
||||
last = chats[-1]
|
||||
next_cursor = _encode_chat_cursor(
|
||||
created_at=last.created_at.fromisoformat(),
|
||||
chat_id=str(last.id),
|
||||
)
|
||||
|
||||
return PageResponse(items=[Chat(
|
||||
id=str(c.id),
|
||||
title=c.title,
|
||||
member_ids=[str(m.user_id) for m in c.members],
|
||||
created_at=c.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z")
|
||||
for c in chats],
|
||||
next_cursor=next_cursor,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@router.get("/v1/chats/{chat_id}", response_model=Chat)
|
||||
async def get_chat(current_user: CurrentUser, session: DbSession, chat_id: UUID) -> Chat:
|
||||
repo = Repository(session)
|
||||
|
||||
chat = await repo.chats.get_by_id_with_members(chat_id)
|
||||
if not chat:
|
||||
raise HTTPException(status_code=404, detail="Chat not found")
|
||||
|
||||
member_ids = {str(m.user_id) for m in chat.members}
|
||||
if str(current_user.id) not in member_ids:
|
||||
raise HTTPException(status_code=404, detail="Chat not found")
|
||||
|
||||
return Chat(
|
||||
id=str(chat.id),
|
||||
title=chat.title,
|
||||
member_ids=[str(m.user_id) for m in chat.members],
|
||||
created_at=chat.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z",
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter
|
||||
from src.pydantic_valid.errors import Health
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/live", response_model=Health, status_code=200)
|
||||
async def healh_live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/ready", response_model=Health, status_code=200)
|
||||
async def health_ready():
|
||||
return {"status": "ok"}
|
||||
@@ -0,0 +1,121 @@
|
||||
import base64
|
||||
from fastapi import HTTPException, APIRouter, Query, status, Request
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
from src.pydantic_valid.message import Message, CreateMessage
|
||||
from src.pydantic_valid.page_response import PageResponse
|
||||
from src.api.dependencies import DbSession, CurrentUser
|
||||
from src.api.repositories.main_repo import Repository
|
||||
from src.services.idempotency import get_cached, body_hash, save_cached
|
||||
from src.utils import generate_id, get_now_iso
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _encode_msg_cursor(chat_id: str, created_at: str, msg_id: str) -> str:
|
||||
raw = f"m|{chat_id}|{created_at}|{msg_id}"
|
||||
return base64.urlsafe_b64encode(raw.encode("utf-8")).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def _decode_msg_cursor(scope_chat_id: str, cursor: str) -> tuple[str, str]:
|
||||
try:
|
||||
padded = cursor + "=" * (-len(cursor) % 4)
|
||||
raw = base64.urlsafe_b64decode(padded).decode("utf-8")
|
||||
parts = raw.split("|", 3)
|
||||
if len(parts) != 4 or parts[0] != "m":
|
||||
raise ValueError("bad cursor structure")
|
||||
chat_id, created_at, msg_id = parts[1], parts[2], parts[3]
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail="invalid cursor") from e
|
||||
|
||||
if scope_chat_id != chat_id:
|
||||
raise HTTPException(status_code=400, detail="cursor from another chat")
|
||||
|
||||
return created_at, msg_id
|
||||
|
||||
|
||||
@router.post("/v1/chats/{chat_id}/messages", response_model=Message, status_code=201)
|
||||
async def send_message(chat_id: UUID, msg_in: CreateMessage, session: DbSession, current_user: CurrentUser, request: Request):
|
||||
repo = Repository(session)
|
||||
|
||||
idempotency_key = request.headers.get("Idempotency-Key")
|
||||
if not idempotency_key:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Idempotency-Key is required")
|
||||
|
||||
chat = await repo.chats.get_by_id_with_members(chat_id)
|
||||
if not chat:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="chat not found")
|
||||
|
||||
if not await repo.chats.is_user_member(chat_id, current_user.id):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="chat not found")
|
||||
|
||||
current_hash = body_hash(msg_in.model_dump())
|
||||
cached = await get_cached(str(current_user.id), str(chat_id), idempotency_key)
|
||||
|
||||
if cached is not None:
|
||||
if cached["body_hash"] != current_hash:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Idempotency-Key is already using with different body")
|
||||
else:
|
||||
return cached["response"]
|
||||
|
||||
message = await repo.messages.create(chat_id=chat_id,
|
||||
sender_id=current_user.id,
|
||||
text=msg_in.text)
|
||||
|
||||
response_data = Message(
|
||||
id=str(message.id),
|
||||
chat_id=str(message.chat_id),
|
||||
sender_id=str(message.sender_id),
|
||||
text=message.text,
|
||||
created_at=message.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z"
|
||||
)
|
||||
|
||||
response_dict = response_data.model_dump()
|
||||
|
||||
await save_cached(
|
||||
str(current_user.id),
|
||||
str(chat_id),
|
||||
idempotency_key,
|
||||
current_hash,
|
||||
response_dict
|
||||
)
|
||||
|
||||
return response_dict
|
||||
|
||||
|
||||
@router.get("/v1/chats/{chat_id}/messages", response_model=PageResponse, status_code=200)
|
||||
async def list_messages(session: DbSession, current_user: CurrentUser, chat_id: UUID, limit: int = Query(default=50, ge=1, le=100), cursor: Optional[str] = Query(default=None, min_length=1)):
|
||||
repo = Repository(session)
|
||||
|
||||
chat = await repo.chats.get_by_id_with_members(chat_id)
|
||||
if not chat:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="chat not found")
|
||||
|
||||
member_ids = {str(m.user_id) for m in chat.members}
|
||||
|
||||
if str(current_user.id) not in member_ids:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="chat not found")
|
||||
cursor_created_at = None
|
||||
cursor_msg_id = None
|
||||
if cursor:
|
||||
created_at_str, msg_id_str = _decode_msg_cursor(str(chat_id), cursor)
|
||||
cursor_created_at = datetime.fromisoformat(created_at_str)
|
||||
cursor_msg_id = UUID(msg_id_str)
|
||||
|
||||
messages, has_more = await repo.messages.get_chat_messages(chat_id=chat_id, limit=limit, cursor_created_at=cursor_created_at, cursor_msg_id=cursor_msg_id)
|
||||
|
||||
next_cursor = None
|
||||
if has_more and messages:
|
||||
last = messages[-1]
|
||||
next_cursor = _encode_msg_cursor(chat_id=str(chat_id), created_at=last.created_at.isoformat(), msg_id=str(last.id))
|
||||
|
||||
return PageResponse(
|
||||
items=[Message(id=str(msg.id), chat_id=str(msg.chat_id), sender_id=str(msg.sender_id), text=msg.text, created_at=msg.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z")
|
||||
for msg in messages],
|
||||
next_cursor=next_cursor,
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from src.pydantic_valid.user import CreateUser, User as UserSchema
|
||||
from src.models.users import User
|
||||
from src.api.repositories.main_repo import Repository
|
||||
from src.auth.password import hash_password
|
||||
from src.api.dependencies import DbSession, CurrentUser
|
||||
from src.utils import get_now_iso
|
||||
|
||||
|
||||
router = APIRouter(prefix="/v1")
|
||||
|
||||
|
||||
@router.post("/users", response_model=UserSchema, status_code=201)
|
||||
async def register_user(body: CreateUser, session: DbSession) -> UserSchema:
|
||||
repo = Repository(session)
|
||||
existing_user = await repo.users.get_by_username(body.username)
|
||||
if existing_user:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="username already exists")
|
||||
|
||||
password_hash = hash_password(body.password)
|
||||
|
||||
new_user = await repo.users.create(
|
||||
username = body.username,
|
||||
display_name = body.display_name,
|
||||
password_hash = password_hash,
|
||||
)
|
||||
|
||||
if not new_user:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="username already exists")
|
||||
|
||||
return UserSchema(
|
||||
id=str(new_user.id),
|
||||
username=new_user.username,
|
||||
display_name=new_user.display_name,
|
||||
created_at=new_user.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/users/me", response_model=UserSchema, status_code=200)
|
||||
async def get_me(current_user: CurrentUser) -> UserSchema:
|
||||
|
||||
return UserSchema(
|
||||
id=str(current_user.id),
|
||||
username=current_user.username,
|
||||
display_name=current_user.display_name,
|
||||
created_at=current_user.created_at.strftime("%Y-%m-%dT%H:%M:%S.%f") + "Z",
|
||||
)
|
||||
@@ -0,0 +1,18 @@
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerifyMismatchError
|
||||
|
||||
|
||||
ph = PasswordHasher()
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return ph.hash(password)
|
||||
|
||||
|
||||
def verify_password(password: str, _hash: str) -> bool:
|
||||
try:
|
||||
ph.verify(_hash, password)
|
||||
return True
|
||||
except VerifyMismatchError:
|
||||
return False
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import secrets
|
||||
import hashlib
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
|
||||
def generate_token() -> tuple[str, str]:
|
||||
raw_token = secrets.token_urlsafe(32)
|
||||
token_hash = hashlib.sha256(raw_token.encode()).hexdigest()
|
||||
return raw_token, token_hash
|
||||
|
||||
|
||||
def hash_token(raw_token: str) -> str:
|
||||
return hashlib.sha256(raw_token.encode()).hexdigest()
|
||||
|
||||
|
||||
def calculate_expires_at(ttl_seconds: int) -> datetime:
|
||||
return datetime.now(timezone.utc) + timedelta(seconds=ttl_seconds)
|
||||
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
from pydantic import Field
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
APP_NAME: str = "MTUSI Messenger"
|
||||
APP_DEBUG: bool = False
|
||||
|
||||
DATABASE_URL: str = Field(..., description="Строка подключения к PostgreSQL. docker_compose перезапишет поле через .env")
|
||||
SESSION_TTL_SECONDS: int = 3600
|
||||
|
||||
DB_POOL_SIZE: int = 10
|
||||
DB_MAX_OVERFLOW: int = 20
|
||||
|
||||
DB_POOL_TIMEOUT: int = 30
|
||||
DB_POOL_RECYCLE: int = 3600
|
||||
|
||||
model_config = SettingsConfigDict(env_file=".env", case_sensitive=False, extra="ignore")
|
||||
|
||||
|
||||
settings = Settings()
|
||||
@@ -0,0 +1,36 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from src.config import settings
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=settings.APP_DEBUG,
|
||||
pool_size=settings.DB_POOL_SIZE,
|
||||
max_overflow=settings.DB_MAX_OVERFLOW,
|
||||
pool_timeout=settings.DB_POOL_TIMEOUT,
|
||||
pool_recycle=settings.DB_POOL_RECYCLE,
|
||||
pool_pre_ping=True,
|
||||
)
|
||||
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession, expire_on_commit=False, autoflush=False
|
||||
)
|
||||
|
||||
|
||||
async def get_db():
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
||||
async def close_engine() -> None:
|
||||
await engine.dispose()
|
||||
@@ -0,0 +1,28 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy import text
|
||||
|
||||
from src.database import engine, close_engine
|
||||
from src.migrations.runner import run_migrations
|
||||
from src.redis_client import init_redis, close_redis
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
|
||||
print("lifespan startup: applying migrations...")
|
||||
await run_migrations()
|
||||
|
||||
print("[lifespan] startup: initializing Redis cache...")
|
||||
await init_redis()
|
||||
|
||||
print("lifespan startup: initializing database engine...")
|
||||
async with engine.connect() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
print("lifespan startup: database ready")
|
||||
|
||||
yield
|
||||
|
||||
print("lifespan shutdown")
|
||||
await close_redis()
|
||||
await close_engine()
|
||||
print("lifespan shutdown: complete")
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
import os
|
||||
import uuid
|
||||
from src.utils import generate_id, create_error_response
|
||||
from src.api.routes.health import health
|
||||
from src.api.routes.users import user
|
||||
from src.api.routes.chats import chat
|
||||
from src.api.routes.messages import message
|
||||
from src.api.routes.auth import auth
|
||||
from src.lifespan import lifespan
|
||||
from sqlalchemy.exc import OperationalError as SAOperationalError
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
|
||||
INSTANCE_ID = os.getenv("INSTANCE_ID") or f"api-{uuid.uuid4().hex[:8]}"
|
||||
|
||||
application = FastAPI(title="MTUSI Messenger", version="1.0.0", lifespan=lifespan)
|
||||
|
||||
|
||||
@application.middleware("http")
|
||||
async def add_global_headers(request: Request, call_next):
|
||||
request_id = str(generate_id())
|
||||
request.state.request_id = request_id
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||
|
||||
return response
|
||||
|
||||
@application.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
messages = []
|
||||
for err in exc.errors():
|
||||
loc = '.'.join(str(p) for p in err["loc"] if p not in ('body',))
|
||||
msg = err.get("msg", "invalid")
|
||||
if msg.startswith("Value error, "):
|
||||
msg = msg[len("Value error, "):]
|
||||
messages.append(f"{loc}: {msg}" if loc else msg)
|
||||
detail = "; ".join(messages) or "invalid request"
|
||||
request_id = getattr(request.state, "request_id", "")
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"error": {
|
||||
"code": "invalid_request",
|
||||
"message": detail,
|
||||
"request_id": request_id
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": INSTANCE_ID},
|
||||
)
|
||||
|
||||
@application.exception_handler(SAOperationalError)
|
||||
async def db_unavailable_handler(request: Request, exc: SAOperationalError):
|
||||
request_id = getattr(request.state, "request_id", str(generate_id()))
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={
|
||||
"error": {
|
||||
"code": "unavailable",
|
||||
"message": "database unavailable",
|
||||
"request_id": request_id,
|
||||
}
|
||||
},
|
||||
headers={"X-Request-Id": request_id, "X-Instance": INSTANCE_ID},
|
||||
)
|
||||
|
||||
@application.exception_handler(HTTPException)
|
||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||
request_id = getattr(request.state, "request_id", None)
|
||||
if request_id is None:
|
||||
request_id = str(generate_id())
|
||||
|
||||
code_map = {
|
||||
400: "invalid_request",
|
||||
401: "unauthorized",
|
||||
404: "not_found",
|
||||
409: "conflict",
|
||||
413: "payload_too_large",
|
||||
415: "unsupported_media_type",
|
||||
429: "rate_limited",
|
||||
503: "unavailable"
|
||||
}
|
||||
|
||||
code = code_map.get(exc.status_code, "internal_error")
|
||||
response_headers = {"X-Request-Id": request_id, "X-Instance-Id": INSTANCE_ID}
|
||||
if exc.headers:
|
||||
response_headers.update(exc.headers)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"error": {
|
||||
"code": code,
|
||||
"message": str(exc.detail),
|
||||
"request_id": request_id}
|
||||
},
|
||||
headers=response_headers)
|
||||
|
||||
@application.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
request_id = getattr(request.state, "request_id", None)
|
||||
if request_id is None:
|
||||
request_id = str(generate_id())
|
||||
print(f'Internal Error: {exc}')
|
||||
return create_error_response(message="Internal Server Error", code="internal_error", status_code=500, request_id=request_id, instance_id=INSTANCE_ID)
|
||||
|
||||
|
||||
|
||||
application.include_router(health.router, prefix="/health", tags=["Health"])
|
||||
application.include_router(user.router, prefix="/api", tags=["Users"])
|
||||
application.include_router(chat.router, prefix="/api", tags=["Chats"])
|
||||
application.include_router(message.router, prefix="/api", tags=["Messages"])
|
||||
application.include_router(auth.router, prefix="/api", tags=["Auth"])
|
||||
@@ -0,0 +1,63 @@
|
||||
BEGIN;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY,
|
||||
username VARCHAR(32) NOT NULL,
|
||||
display_name VARCHAR(100) NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT users_username_unique UNIQUE (username),
|
||||
CONSTRAINT users_username_pattern CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_length CHECK (
|
||||
char_length(display_name) BETWEEN 1 AND 100
|
||||
AND trim(display_name) <> ''
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash CHAR(64) PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS sessions_user_id_idx ON sessions(user_id);
|
||||
CREATE INDEX IF NOT EXISTS sessions_expires_at_idx ON sessions(expires_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT chats_title_length CHECK (
|
||||
char_length(title) BETWEEN 1 AND 100
|
||||
AND trim(title) <> ''
|
||||
)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_id_idx ON chat_members(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT messages_text_length CHECK (
|
||||
char_length(text) BETWEEN 1 AND 4000
|
||||
AND trim(text) <> ''
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS messages_chat_created_id_idx
|
||||
ON messages(chat_id, created_at, id);
|
||||
|
||||
COMMIT;
|
||||
@@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
import asyncpg
|
||||
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).parent
|
||||
|
||||
|
||||
async def run_migrations() -> None:
|
||||
dsn = os.environ.get("DATABASE_URL")
|
||||
if not dsn:
|
||||
raise RuntimeError("DATABASE_URL environment is not set")
|
||||
|
||||
if dsn.startswith("postgresql+asyncpg://"):
|
||||
dsn = dsn.replace("postgresql+asyncpg://", "postgresql://", 1)
|
||||
|
||||
print(f"Connecting to DB")
|
||||
conn = await asyncpg.connect(dsn)
|
||||
try:
|
||||
sql_files = sorted(MIGRATIONS_DIR.glob("*.sql"))
|
||||
if not sql_files:
|
||||
print("no SQl files")
|
||||
return
|
||||
for sql_file in sql_files:
|
||||
print(f"migration applying {sql_file.name}")
|
||||
sql = sql_file.read_text(encoding="utf-8")
|
||||
await conn.execute(sql)
|
||||
print(f"mighration applied succesful to {sql_file.name}")
|
||||
print(f"All {len(sql_files)} migrations done")
|
||||
|
||||
finally:
|
||||
await conn.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_migrations())
|
||||
@@ -0,0 +1,70 @@
|
||||
# #in_memory
|
||||
# from src.models.users import UsersRepository
|
||||
# from src.models.chats import ChatsRepository
|
||||
# from src.models.messages import MessagesRepository
|
||||
# import asyncio
|
||||
# import uuid
|
||||
# from src.pydantic_valid.user import User
|
||||
# from src.pydantic_valid.chat import Chat
|
||||
# from src.pydantic_valid.message import Message
|
||||
|
||||
|
||||
# class Repository():
|
||||
# def __init__(self):
|
||||
# self.users = UsersRepository()
|
||||
# self.chats = ChatsRepository()
|
||||
# self.messages = MessagesRepository()
|
||||
# self.username_index = UsernameIndexRepository()
|
||||
# #self._lock = asyncio.Lock() #pessimistic locking против race condition
|
||||
# self._user_lock = asyncio.Lock()
|
||||
# self._chats_locks_guard = asyncio.Lock()
|
||||
# self._chat_locks : dict[str, asyncio.Lock] = {}
|
||||
|
||||
|
||||
# async def insert_user(self, user: User) -> bool:
|
||||
# key = user.username.lower()
|
||||
# async with self._user_lock: #избегания создания дубликатов данных юзера в self.users под разными индексами | в username_index 1 запись на все данные в то же время
|
||||
# if key in self.username_index.username_index_db:
|
||||
# return False
|
||||
# self.username_index.username_index_db[key] = user.id
|
||||
# self.users.users_db[user.id] = user
|
||||
# return True
|
||||
|
||||
|
||||
# async def get_user(self, uid: uuid.UUID) -> User:
|
||||
# user_data = self.users.users_db.get(str(uid))
|
||||
# if user_data:
|
||||
# return user_data #"id": str(uid), for User model response else UserOut model
|
||||
# return None
|
||||
|
||||
|
||||
# async def _get_chat_lock(self, chat_id: str) -> asyncio.Lock:
|
||||
# async with self._chats_locks_guard:
|
||||
# if chat_id not in self._chat_locks:
|
||||
# self._chat_locks[chat_id] = asyncio.Lock()
|
||||
# return self._chat_locks[chat_id]
|
||||
|
||||
|
||||
# async def create_chat(self, chat_data: Chat) -> bool:
|
||||
# chat_id = chat_data.id
|
||||
# lock = await self._get_chat_lock(chat_id)
|
||||
# async with lock:
|
||||
# if chat_id in self.chats.chats_db:
|
||||
# return False
|
||||
# else:
|
||||
# self.chats.chats_db[chat_id] = chat_data.model_dump()
|
||||
# self.messages.message_db[chat_id] = []
|
||||
# return True
|
||||
|
||||
|
||||
# async def create_message(self, message_data: Message) -> bool:
|
||||
# chat_id = message_data.chat_id
|
||||
# lock = await self._get_chat_lock(chat_id)
|
||||
# async with lock:
|
||||
# if chat_id in self.messages.message_db:
|
||||
# # self.messages.message_db[chat_id].append(message_data.model_dump())
|
||||
# await self.messages.append_message(chat_id, message_data.model_dump())
|
||||
# return True
|
||||
# else:
|
||||
# return False
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy import String, DateTime, ForeignKey
|
||||
from sqlalchemy.sql import text as sqltext
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from src.database import Base
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.models.users import User
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4)
|
||||
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sqltext("now()"))
|
||||
members: Mapped[list["ChatMember"]] = relationship("ChatMember",
|
||||
cascade="all, delete-orphan",
|
||||
back_populates="chat")
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Chat(id={self.id}, title={self.title})>"
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True),
|
||||
ForeignKey("chats.id", ondelete="CASCADE"),
|
||||
primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="CASCADE"),
|
||||
primary_key=True)
|
||||
|
||||
user: Mapped["User"] = relationship("User")
|
||||
chat: Mapped["Chat"] = relationship("Chat", back_populates="members")
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<ChatMember(chat_id={self.chat_id}, user_id={self.user_id})>"
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, DateTime, ForeignKey, Text
|
||||
from sqlalchemy.sql import text as sql_text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from src.database import Base
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
default=uuid.uuid4
|
||||
)
|
||||
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("chats.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
|
||||
sender_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="RESTRICT"),
|
||||
nullable=False
|
||||
)
|
||||
|
||||
text: Mapped[str] = mapped_column(
|
||||
Text,
|
||||
nullable=False
|
||||
)
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sql_text("now()")
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Message(id={self.id}, chat_id={self.chat_id}, text={self.text[:20]}...)>"
|
||||
@@ -0,0 +1,31 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from sqlalchemy import String, DateTime, ForeignKey
|
||||
from sqlalchemy.sql import text as sqltext
|
||||
from src.database import Base
|
||||
|
||||
class Session(Base):
|
||||
__tablename__ = "sessions"
|
||||
|
||||
token_hash: Mapped[str] = mapped_column(String(64), primary_key=True, comment="SHA-256 opaque token")
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id", ondelete="CASCADE",),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="Внешний ключ на идентификатор пользователя")
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="время истечения сессии")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True),
|
||||
nullable=False,
|
||||
server_default=sqltext("now()"),
|
||||
comment="время создания сессии")
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Session(token_hash={self.token_hash[:8]},... user_id={self.user_id})>"
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy import Column, String, DateTime, Text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.sql import text as sqltext
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from src.database import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, comment="Первичный ключ как uui4, вероятность коллизии стремится к 0. В будущем лучше переходить к uuid7-время+рандом")
|
||||
username: Mapped[str] = mapped_column(String(32), unique=True, nullable=False, index=True, comment="Логин пользователя")
|
||||
display_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(String, nullable=False, comment="Argon2id hash пароля")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=sqltext("now()"), nullable=False)
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<User(id={self.id}, username={self.username})>"
|
||||
@@ -0,0 +1,38 @@
|
||||
import uuid
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
|
||||
|
||||
class CreateChat(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
title: str = Field(..., min_length=1, max_length=100)
|
||||
member_ids: list[str] = Field(..., min_length=1, max_length=100)
|
||||
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def title_validate(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("title shouldn't have only whitespaces and cannot be blank")
|
||||
return v
|
||||
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def validate_member_ids(cls, v: list[str]) -> list[str]:
|
||||
if len(v) != len(set(v)):
|
||||
raise ValueError("Dublicates are prohibitated")
|
||||
|
||||
for ids in v:
|
||||
try:
|
||||
uuid.UUID(ids)
|
||||
except:
|
||||
raise ValueError(f'invalid UUID format was detected for member_id: {ids}')
|
||||
return v
|
||||
|
||||
|
||||
|
||||
class Chat(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
member_ids: list[str]
|
||||
created_at: str
|
||||
@@ -0,0 +1,15 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ErrorDetail(BaseModel):
|
||||
code: str
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorResponse(BaseModel):
|
||||
detail: ErrorDetail
|
||||
|
||||
|
||||
class Health(BaseModel):
|
||||
status: str
|
||||
@@ -0,0 +1,22 @@
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
text: str = Field(..., min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def validate_text(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("Blank or text consisted only from whitespaces is prohibitated")
|
||||
return v
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
@@ -0,0 +1,7 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
class PageResponse(BaseModel):
|
||||
items: list[Any]
|
||||
next_cursor: Optional[str] = None
|
||||
@@ -0,0 +1,16 @@
|
||||
from pydantic import Field, BaseModel, ConfigDict
|
||||
|
||||
|
||||
class SessionUser(BaseModel):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class SessionCreateResponse(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
token: str
|
||||
token_type: str = Field(default="Bearer", pattern="^Bearer$")
|
||||
expires_at: str
|
||||
user: SessionUser
|
||||
@@ -0,0 +1,23 @@
|
||||
from pydantic import BaseModel, Field, field_validator, ConfigDict
|
||||
|
||||
|
||||
class CreateUser(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(..., min_length=1, max_length=100)
|
||||
password: str = Field(..., min_length=12, max_length=128)
|
||||
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def validate_display_name(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("Name cannot be whitespaces or none")
|
||||
return v
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
@@ -0,0 +1,27 @@
|
||||
import os
|
||||
import redis.asyncio as aioredis
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
REDIS_URL = os.getenv("REDIS_URL")
|
||||
redis_client: aioredis.Redis | None = None
|
||||
|
||||
|
||||
async def init_redis() -> aioredis.Redis:
|
||||
global redis_client
|
||||
redis_client = aioredis.from_url(REDIS_URL, decode_responses=True, socket_connect_timeout=2, socket_timeout=2)
|
||||
try:
|
||||
await redis_client.ping()
|
||||
except RedisError:
|
||||
redis_client = None
|
||||
return redis_client
|
||||
|
||||
|
||||
async def close_redis():
|
||||
global redis_client
|
||||
if redis_client:
|
||||
await redis_client.close()
|
||||
redis_client = None
|
||||
|
||||
|
||||
def get_redis() -> aioredis.Redis | None:
|
||||
return redis_client
|
||||
@@ -0,0 +1,41 @@
|
||||
import hashlib
|
||||
import json
|
||||
from redis.exceptions import RedisError
|
||||
from src.redis_client import get_redis
|
||||
|
||||
IDEMPOTENCY_PREFIX = "idempotency:"
|
||||
IDEMPOTENCY_TTL = 86400
|
||||
|
||||
|
||||
def _key(user_id: str, chat_id: str, idempotency_key: str) -> str:
|
||||
return f"{IDEMPOTENCY_PREFIX}{user_id}:{chat_id}:{idempotency_key}"
|
||||
|
||||
|
||||
def body_hash(body: dict) -> str:
|
||||
canonical = json.dumps(body, sort_keys=True, ensure_ascii=False, separators=(',', ':'))
|
||||
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
async def get_cached(user_id: str, chat_id: str, idempotency_key: str) -> dict | None:
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return None
|
||||
try:
|
||||
raw = await r.get(_key(user_id, chat_id, idempotency_key))
|
||||
return json.loads(raw) if raw else None
|
||||
except (RedisError, json.JSONDecodeError):
|
||||
return None
|
||||
|
||||
|
||||
async def save_cached(user_id: str, chat_id: str, idempotency_key: str, body_h: str, response: dict) -> None:
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return
|
||||
try:
|
||||
await r.setex(
|
||||
_key(user_id, chat_id, idempotency_key),
|
||||
IDEMPOTENCY_TTL,
|
||||
json.dumps({"body_hash": body_h, "response": response})
|
||||
)
|
||||
except RedisError:
|
||||
pass
|
||||
@@ -0,0 +1,78 @@
|
||||
import uuid
|
||||
import random
|
||||
from src.redis_client import get_redis
|
||||
from redis.exceptions import RedisError
|
||||
from src.config import settings
|
||||
|
||||
|
||||
SESSION_PREFIX = "session:"
|
||||
LOCK_PREFIX = "lock:session:"
|
||||
CACHE_TTL_JITTER = 0.1
|
||||
|
||||
|
||||
def _key(token_hash: str) -> str:
|
||||
return f"{SESSION_PREFIX}{token_hash}"
|
||||
|
||||
|
||||
def _lock_key(token_hash: str) -> str:
|
||||
return f"{LOCK_PREFIX}{token_hash}"
|
||||
|
||||
|
||||
async def cache_session(token_hash: str, user_id: uuid.UUID, ttl: int | None = None) -> None:
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return
|
||||
|
||||
effective_ttl = ttl or settings.SESSION_TTL_SECONDS
|
||||
jitter = int(effective_ttl * CACHE_TTL_JITTER * (2 * random.random() - 1))
|
||||
final_ttl = max(1, effective_ttl + jitter)
|
||||
|
||||
try:
|
||||
await r.setex(_key(token_hash), final_ttl, str(user_id))
|
||||
except RedisError:
|
||||
pass
|
||||
|
||||
|
||||
async def get_cached_user_id(token_hash: str) -> uuid.UUID | None:
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return
|
||||
|
||||
try:
|
||||
value = await r.get(_key(token_hash))
|
||||
return uuid.UUID(value) if value else None
|
||||
except RedisError:
|
||||
return None
|
||||
|
||||
|
||||
async def invalidate_session(token_hash: str) -> None:
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return
|
||||
|
||||
try:
|
||||
await r.delete(_key(token_hash))
|
||||
except RedisError:
|
||||
pass
|
||||
|
||||
|
||||
async def invalidate_all_user_sessions(user_id: uuid.UUID) -> int:
|
||||
|
||||
r = get_redis()
|
||||
if not r:
|
||||
return 0
|
||||
|
||||
try:
|
||||
pattern = f"{SESSION_PREFIX}*"
|
||||
keys = []
|
||||
async for key in r.scan_iter(match=pattern, count=100):
|
||||
value = r.get(key)
|
||||
if value and uuid.UUID(value) == user_id:
|
||||
keys.append(key)
|
||||
|
||||
if keys:
|
||||
await r.delete(*keys)
|
||||
return len(keys)
|
||||
|
||||
except RedisError:
|
||||
return 0
|
||||
@@ -0,0 +1,16 @@
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
def generate_id() -> str:
|
||||
return uuid.uuid4()
|
||||
|
||||
|
||||
def get_now_iso() -> str:
|
||||
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
|
||||
def create_error_response(message: str, code: str, status_code: int, request_id: str, instance_id: str) -> JSONResponse:
|
||||
body = {"error": {"code": code, "message": message, "request_id": request_id}}
|
||||
return JSONResponse(status_code=status_code, content=body, headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id})
|
||||
@@ -0,0 +1,68 @@
|
||||
import os
|
||||
import pytest
|
||||
import uuid
|
||||
import requests
|
||||
import base64
|
||||
|
||||
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
||||
TEST_PASSWORD = "SecurePassword7654321"
|
||||
|
||||
|
||||
def create_and_login_user(base_url, display_name_prefix):
|
||||
username = f"st_{display_name_prefix.lower()}_{uuid.uuid4().hex[:8]}"
|
||||
|
||||
registr_response = requests.post(f"{base_url}/api/v1/users",
|
||||
json={
|
||||
"username": username,
|
||||
"display_name": f"{display_name_prefix} Test",
|
||||
"password": TEST_PASSWORD})
|
||||
assert registr_response.status_code == 201, f"Registration failed: {registr_response.text}"
|
||||
user_data = registr_response.json()
|
||||
|
||||
credentials = base64.b64encode(f"{username}:{TEST_PASSWORD}".encode()).decode()
|
||||
login_response = requests.post(f"{base_url}/api/v1/auth/sessions",
|
||||
headers={"Authorization": f"Basic {credentials}"})
|
||||
assert login_response.status_code == 201, f"Login failed: {login_response.text}"
|
||||
token = login_response.json()["token"]
|
||||
|
||||
return {
|
||||
"id": user_data["id"],
|
||||
"username": username,
|
||||
"user": user_data,
|
||||
"headers": {"Authorization": f"Bearer {token}"}
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def base_url():
|
||||
return BASE_URL
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def alice(base_url):
|
||||
return create_and_login_user(base_url, "Alice")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def bob(base_url):
|
||||
return create_and_login_user(base_url, "Bob")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def eve(base_url):
|
||||
return create_and_login_user(base_url, "Eve")
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def chat(base_url, alice, bob):
|
||||
|
||||
response = requests.post(f"{base_url}/api/v1/chats", headers=alice["headers"], json={
|
||||
"title": "Student Test Chat",
|
||||
"member_ids": [alice["id"], bob["id"]]
|
||||
})
|
||||
assert response.status_code == 201, response.text
|
||||
return response.json()
|
||||
|
||||
def auth(actor):
|
||||
return actor["headers"]
|
||||
@@ -0,0 +1,48 @@
|
||||
import requests
|
||||
import uuid
|
||||
from conftest import base_url, auth
|
||||
|
||||
|
||||
def test_foreign_chat_get(base_url, eve, chat):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat['id']}",
|
||||
headers=auth(eve))
|
||||
assert response.status_code == 404
|
||||
assert response.json()["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
def test_foreign_chat_messages(base_url, eve, chat):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat['id']}/messages",
|
||||
headers=auth(eve))
|
||||
assert response.status_code == 404
|
||||
assert response.json()["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
def test_foreign_chat_send_messages(base_url, eve, chat):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat['id']}/messages",
|
||||
headers={**auth(eve), "Idempotency-Key": str(uuid.uuid4())},
|
||||
json={"text": "Hello, I miss you"},
|
||||
)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
def test_foreign_chat_not_in_list(base_url, eve, chat):
|
||||
response = requests.get(f"{base_url}/api/v1/chats?limit=100", headers=auth(eve))
|
||||
|
||||
assert response.status_code == 200
|
||||
chat_ids = [c["id"] for c in response.json()["items"]]
|
||||
assert chat["id"] not in chat_ids
|
||||
|
||||
|
||||
def test_invalid_chat_id_format(base_url, alice):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/wrong_id",
|
||||
headers=auth(alice))
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_nonexistent_chat(base_url, alice):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/00000000-0000-0000-0000-000000000000",
|
||||
headers=auth(alice))
|
||||
assert response.status_code == 404
|
||||
assert response.json()["error"]["code"] == "not_found"
|
||||
@@ -0,0 +1,76 @@
|
||||
import requests
|
||||
import uuid
|
||||
from conftest import base_url, auth
|
||||
|
||||
|
||||
def test_multi_page_walktrhough(base_url, alice, chat):
|
||||
sent = []
|
||||
for i in range(7):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat['id']}/messages",
|
||||
headers={**auth(alice), "Idempotency-Key": str(uuid.uuid4())},
|
||||
json={"text": f"page-test-{i}-{uuid.uuid4().hex[:6]}"})
|
||||
assert response.status_code == 201, response.text
|
||||
sent.append(response.json())
|
||||
|
||||
all_items = []
|
||||
cursor = None
|
||||
pages_seen = 0
|
||||
|
||||
while True:
|
||||
url = f"{base_url}/api/v1/chats/{chat['id']}/messages?limit=2"
|
||||
if cursor:
|
||||
url += f"&cursor={cursor}"
|
||||
response = requests.get(url, headers=auth(alice))
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
|
||||
items = body["items"]
|
||||
assert len(items) <= 2
|
||||
all_items.extend(items)
|
||||
pages_seen +=1
|
||||
|
||||
next_cursor = body["next_cursor"]
|
||||
if next_cursor is None:
|
||||
break
|
||||
cursor = next_cursor
|
||||
|
||||
assert pages_seen == 4, f"expected 4 pages, got {pages_seen}"
|
||||
|
||||
ids = [m["id"] for m in all_items]
|
||||
assert len(ids) == len(set(ids)), "duplicate in pages"
|
||||
|
||||
keys = [(m["created_at"], m["id"]) for m in all_items]
|
||||
assert keys == sorted(keys), "messages not in ascending order (created_at, id)"
|
||||
|
||||
our_ids = {m["id"] for m in sent}
|
||||
assert our_ids.issubset(set(ids)), "some sent messages are missing from pagination"
|
||||
|
||||
|
||||
def test_cursor_from_another_chat_rejected(base_url, alice, eve, chat):
|
||||
response = requests.post(f"{base_url}/api/v1/chats",
|
||||
headers=auth(alice),
|
||||
json={
|
||||
"title": "Second Chat",
|
||||
"member_ids": [alice["id"], eve["id"]],
|
||||
})
|
||||
assert response.status_code == 201
|
||||
chat2 = response.json()
|
||||
|
||||
for _ in range(2):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat2['id']}/messages",
|
||||
headers={**auth(alice), "Idempotency-Key": str(uuid.uuid4())},
|
||||
json={"text": "Hello, I miss you"})
|
||||
assert response.status_code == 201
|
||||
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat2['id']}/messages?limit=1",
|
||||
headers=auth(alice))
|
||||
assert response.status_code == 200
|
||||
cursor = response.json().get("next_cursor")
|
||||
|
||||
if cursor is None:
|
||||
return
|
||||
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat['id']}/messages?limit=1&cursor={cursor}",
|
||||
headers=auth(alice))
|
||||
assert response.status_code == 400, f"expected 400 for cross-chat cursor, got {response.status_code}"
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
@@ -0,0 +1,109 @@
|
||||
import requests
|
||||
from student_tests.conftest import base_url, auth
|
||||
|
||||
|
||||
def test_malformed_json(base_url):
|
||||
response = requests.post(f"{base_url}/api/v1/users", headers={
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
data=b"not valid json")
|
||||
|
||||
assert response.status_code == 400
|
||||
body = response.json()
|
||||
assert body["error"]["code"] == "invalid_request"
|
||||
assert body["error"]["request_id"] == response.headers["X-Request-Id"]
|
||||
|
||||
|
||||
def test_wrong_field(base_url):
|
||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
||||
"username": 1235,
|
||||
"display_name": "Alice",
|
||||
})
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_extra_fields(base_url):
|
||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
||||
"username": "extra_field",
|
||||
"display_name": "Test",
|
||||
"Extra": "Should be rejected"
|
||||
})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_empty_message(base_url, alice, chat):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat["id"]}/messages",
|
||||
headers=auth(alice),
|
||||
json={"text": ""})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_whitespaces_message(base_url, alice, chat):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat["id"]}/messages",
|
||||
headers=auth(alice),
|
||||
json={"text": " \t\n \t"})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_too_large_message(base_url, alice, chat):
|
||||
response = requests.post(f"{base_url}/api/v1/chats/{chat["id"]}/messages",
|
||||
headers=auth(alice),
|
||||
json={"text": "0" * 4001})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_duplicate_username(base_url, alice):
|
||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
||||
"username": alice["username"],
|
||||
"display_name": "Another Alice",
|
||||
"password": "SecurePassword7654321"}
|
||||
)
|
||||
|
||||
assert response.status_code == 409
|
||||
assert response.json()["error"]["code"] == "conflict"
|
||||
|
||||
|
||||
def test_invalid_username_pattern(base_url):
|
||||
response = requests.post(f"{base_url}/api/v1/users", json={
|
||||
"username": "AliceWithCaps",
|
||||
"display_name": "Test",
|
||||
})
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_missing_x_user_id(base_url):
|
||||
response = requests.get(f"{base_url}/api/v1/users/me")
|
||||
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
def test_unknown_user_id(base_url):
|
||||
response = requests.get(f"{base_url}/api/v1/users/me",
|
||||
headers={"X-User-Id": "00000000-0000-0000-0000-000000000000"})
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == "unauthorized"
|
||||
|
||||
|
||||
def test_ivalid_limit_zero(base_url, alice, chat):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat["id"]}/messages?limit=0",
|
||||
headers=auth(alice),
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
|
||||
|
||||
def test_invalid_limit_over(base_url, alice, chat):
|
||||
response = requests.get(f"{base_url}/api/v1/chats/{chat["id"]}/messages?limit=101",
|
||||
headers=auth(alice),
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"]["code"] == "invalid_request"
|
||||
Reference in New Issue
Block a user