Files
mtusi2026lab4/src/health/health.controller.ts
T

45 lines
1.1 KiB
TypeScript

import { Controller, Get } from '@nestjs/common';
import { ApiError } from '../common/filters/api-error';
import { PrismaService } from '../db/prisma.service';
import { S3Service } from '../storage/s3.service';
import { isDraining } from './shutdown';
@Controller('health')
export class HealthController {
constructor(
private readonly prisma: PrismaService,
private readonly s3: S3Service,
) {}
@Get('live')
live(): { status: 'ok' } {
return { status: 'ok' };
}
@Get('core')
async core(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
return { status: 'ok' };
}
@Get('ready')
async ready(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
if (!(await this.s3.isAvailable())) {
throw new ApiError('unavailable', 'object storage unavailable');
}
return { status: 'ok' };
}
private async assertCoreReady(): Promise<void> {
if (isDraining()) {
throw new ApiError('unavailable', 'shutting down');
}
try {
await this.prisma.$queryRaw`SELECT 1`;
} catch {
throw new ApiError('unavailable', 'database unavailable');
}
}
}