lab4: стенд MinIO, секреты, bootstrap, хуки; перенос приложения lab3
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { HttpAdapterHost } from '@nestjs/core';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module.js';
|
||||
import { HealthService } from './health/health.service.js';
|
||||
import { MetricsService } from './metrics/metrics.service.js';
|
||||
import { RequestIdInterceptor } from './common/http/request-id.interceptor.js';
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter({ logger: true }),
|
||||
);
|
||||
app.useGlobalInterceptors(
|
||||
new RequestIdInterceptor(
|
||||
app.get(HttpAdapterHost),
|
||||
app.get(MetricsService),
|
||||
app.get(ConfigService),
|
||||
),
|
||||
);
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true,
|
||||
}),
|
||||
);
|
||||
|
||||
const config = app.get(ConfigService);
|
||||
const port = config.getOrThrow<number>('APP_PORT');
|
||||
const host = config.getOrThrow<string>('APP_HOST');
|
||||
await app.listen(port, host);
|
||||
|
||||
// Drain before teardown. Nest's own shutdown sequence destroys providers
|
||||
// (closing the database pool and Redis) before `beforeApplicationShutdown`
|
||||
// runs, so relying on it would keep the replica listed in the load balancer
|
||||
// while it can no longer serve: SIGTERM must first turn the replica
|
||||
// not-ready for the LB probe, then wait DRAIN_DELAY_MS, and only then close
|
||||
// the server and the pools.
|
||||
const health = app.get(HealthService);
|
||||
const drainDelayMs = config.getOrThrow<number>('DRAIN_DELAY_MS');
|
||||
let shuttingDown = false;
|
||||
const shutdown = async (): Promise<void> => {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
health.setDraining();
|
||||
if (drainDelayMs > 0)
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, drainDelayMs));
|
||||
await app.close();
|
||||
process.exit(0);
|
||||
};
|
||||
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'] as const)
|
||||
process.once(signal, () => {
|
||||
void shutdown();
|
||||
});
|
||||
}
|
||||
|
||||
await bootstrap();
|
||||
Reference in New Issue
Block a user