Picsur/backend/src/main.ts

52 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-05-01 21:29:56 +00:00
import fastifyHelmet from '@fastify/helmet';
import multipart from '@fastify/multipart';
2022-03-11 23:14:16 +00:00
import { NestFactory, Reflector } from '@nestjs/core';
2022-02-21 13:53:21 +00:00
import {
2022-06-05 10:20:16 +00:00
FastifyAdapter,
2022-06-27 11:22:48 +00:00
NestFastifyApplication
2022-02-21 13:53:21 +00:00
} from '@nestjs/platform-fastify';
2022-03-06 11:34:33 +00:00
import { AppModule } from './app.module';
2022-04-18 12:34:53 +00:00
import { UsersService } from './collections/user-db/user-db.service';
2022-03-28 10:27:25 +00:00
import { HostConfigService } from './config/early/host.config.service';
2022-04-18 12:34:53 +00:00
import { MainExceptionFilter } from './layers/exception/exception.filter';
2022-02-24 20:32:31 +00:00
import { SuccessInterceptor } from './layers/success/success.interceptor';
2022-04-04 08:36:59 +00:00
import { ZodValidationPipe } from './layers/validate/zod-validator.pipe';
import { PicsurLoggerService } from './logger/logger.service';
2022-03-11 23:14:16 +00:00
import { MainAuthGuard } from './managers/auth/guards/main.guard';
2022-03-31 20:40:11 +00:00
import { HelmetOptions } from './security';
2022-02-21 21:17:44 +00:00
2022-02-21 09:46:03 +00:00
async function bootstrap() {
2022-03-23 16:17:39 +00:00
// Create fasify
2022-02-21 21:17:44 +00:00
const fastifyAdapter = new FastifyAdapter();
2022-03-06 11:27:11 +00:00
// TODO: generic error messages
2022-03-31 20:40:11 +00:00
await fastifyAdapter.register(multipart as any);
2022-06-27 11:22:48 +00:00
await fastifyAdapter.register(fastifyHelmet as any, HelmetOptions);
2022-02-21 21:17:44 +00:00
2022-03-23 16:17:39 +00:00
// Create nest app
2022-02-21 13:53:21 +00:00
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
2022-02-25 12:56:16 +00:00
fastifyAdapter,
{
2022-08-27 15:40:57 +00:00
bufferLogs: true,
2022-03-10 22:02:27 +00:00
},
2022-02-21 13:53:21 +00:00
);
2022-03-23 16:17:39 +00:00
// Configure logger
app.useLogger(app.get(PicsurLoggerService));
app.flushLogs();
2022-02-24 20:32:31 +00:00
app.useGlobalFilters(new MainExceptionFilter());
2022-04-04 08:36:59 +00:00
app.useGlobalInterceptors(new SuccessInterceptor(app.get(Reflector)));
app.useGlobalPipes(new ZodValidationPipe());
2022-03-12 14:10:22 +00:00
app.useGlobalGuards(
2022-03-31 20:40:11 +00:00
new MainAuthGuard(app.get(Reflector), app.get(UsersService)),
2022-03-12 14:10:22 +00:00
);
2022-03-06 11:27:11 +00:00
2022-03-23 16:17:39 +00:00
// Start app
2022-03-06 11:27:11 +00:00
const hostConfigService = app.get(HostConfigService);
await app.listen(hostConfigService.getPort(), hostConfigService.getHost());
2022-02-21 09:46:03 +00:00
}
bootstrap().catch(console.error);