Этот коммит впервые фиксирует порт lab2 (приложение NestJS+Fastify, Dockerfile, одноservice-стенд compose, restart hook) и добавляет уровень 3/4 по lab3: - X-Instance-Id = INSTANCE_ID либо hostname контейнера (стабилен для процесса), добавляется и в ответах об ошибках, которые формируются до интерсептора. - /internal/lb-check: проба LB, намеренно не зависящая от PostgreSQL и Redis (отказ общей БД нельзя лечить выводом реплик из ротации); /health/ready остаётся операторским сигналом. /internal/metrics отдаёт instance_id и build_id. - Общая идемпотентность POST сообщения: таблица idempotency_keys, область (user, method, path, key), канонический отпечаток тела, заявка+результат в одной транзакции, повтор возвращает сохранённый ответ, другое тело — 409, отсутствие/неверный ключ — 400, просрочка по IDEMPOTENCY_TTL_SECONDS. - Транзиентные коды PostgreSQL (57014/40001/40P01) отображаются в 503, чтобы прерывание не фиксировалось идемпотентной записью как окончательный результат. - Миграции 003 (идемпотентность) и 004 (pg_stat_statements для трека 1), операторский прогон GC: npm run idempotency:gc.
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
DefaultValuePipe,
|
|
Get,
|
|
Headers,
|
|
Param,
|
|
ParseIntPipe,
|
|
ParseUUIDPipe,
|
|
Post,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { CurrentUser } from '../common/auth/current-user.decorator.js';
|
|
import { CurrentUserGuard } from '../common/auth/current-user.guard.js';
|
|
import type { User } from '../users/user.js';
|
|
import { CreateMessageDto } from './dto/create-message.dto.js';
|
|
import { MessagesService } from './messages.service.js';
|
|
|
|
@Controller('api/v1/chats/:chatId/messages')
|
|
@UseGuards(CurrentUserGuard)
|
|
export class MessagesController {
|
|
constructor(private readonly messagesService: MessagesService) {}
|
|
|
|
@Post()
|
|
create(
|
|
@Param('chatId', new ParseUUIDPipe()) chatId: string,
|
|
@Body() body: CreateMessageDto,
|
|
@CurrentUser() currentUser: User,
|
|
@Headers('idempotency-key') idempotencyKey?: string | string[],
|
|
) {
|
|
return this.messagesService.create(
|
|
chatId,
|
|
body,
|
|
currentUser,
|
|
idempotencyKey,
|
|
);
|
|
}
|
|
|
|
@Get()
|
|
list(
|
|
@Param('chatId', new ParseUUIDPipe()) chatId: string,
|
|
@CurrentUser() currentUser: User,
|
|
@Query('limit', new DefaultValuePipe(50), new ParseIntPipe())
|
|
limit: number,
|
|
@Query('cursor') cursor?: string,
|
|
) {
|
|
if (limit < 1 || limit > 100)
|
|
throw new BadRequestException('limit must be between 1 and 100');
|
|
return this.messagesService.list(chatId, currentUser, limit, cursor);
|
|
}
|
|
}
|