109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from './app.module';
|
|
import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';
|
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
|
|
import helmet from 'helmet';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { Logger } from 'nestjs-pino';
|
|
import { AdminPanelSetup } from './modules/admin-panel/admin-panel.setup';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule, { bufferLogs: true });
|
|
|
|
app.useLogger(app.get(Logger));
|
|
|
|
app.setGlobalPrefix('v1');
|
|
|
|
// Mount the admin panel before Nest registers its body parsers; the setup
|
|
// itself is deferred until the DI container is ready (after app.init)
|
|
AdminPanelSetup.mount(app);
|
|
|
|
app.useGlobalPipes(
|
|
new I18nValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
transformOptions: {
|
|
enableImplicitConversion: true,
|
|
},
|
|
}),
|
|
);
|
|
|
|
app.useGlobalFilters(
|
|
new I18nValidationExceptionFilter({
|
|
errorFormatter: (errors) => {
|
|
return errors.map((err) => ({
|
|
field: err.property,
|
|
errors: Object.values(err.constraints ?? {}),
|
|
}));
|
|
},
|
|
}),
|
|
);
|
|
|
|
app.useGlobalInterceptors(new ResponseInterceptor());
|
|
|
|
const configService = app.get(ConfigService);
|
|
|
|
app.enableCors({
|
|
origin: true,
|
|
credentials: true,
|
|
});
|
|
|
|
// TODO: Uncomment
|
|
// const origins = configService.get<string[]>('app.corsOrigins');
|
|
// const subdomain = configService.get<string>('app.corsSubdomain');
|
|
|
|
// app.enableCors({
|
|
// origin: (origin: string, callback: any) => {
|
|
// if (!origin) {
|
|
// // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
// return callback(null, true);
|
|
// }
|
|
|
|
// if (origins?.includes(origin)) {
|
|
// // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
// return callback(null, true);
|
|
// }
|
|
|
|
// try {
|
|
// const hostname = new URL(origin).hostname;
|
|
|
|
// if (hostname === subdomain || hostname.endsWith(`.${subdomain}`)) {
|
|
// // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
// return callback(null, true);
|
|
// }
|
|
// } catch {
|
|
// /* empty */
|
|
// }
|
|
|
|
// // eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
// callback(new Error('Not allowed by CORS'));
|
|
// },
|
|
// credentials: true,
|
|
// });
|
|
|
|
app.use(helmet());
|
|
|
|
const config = new DocumentBuilder()
|
|
.setTitle('Heala API')
|
|
.setDescription('Heala API Documentation')
|
|
.setVersion('1.0')
|
|
.setLicense('MIT', 'https://opensource.org/license/mit');
|
|
|
|
const swaggerServers = configService.get<string[]>('app.swaggerServers');
|
|
|
|
swaggerServers?.forEach((server) => config.addServer(server));
|
|
|
|
const document = SwaggerModule.createDocument(app, config.build());
|
|
|
|
SwaggerModule.setup('v1/docs', app, document);
|
|
|
|
await app.init();
|
|
|
|
AdminPanelSetup.setup(app);
|
|
|
|
await app.listen(process.env.PORT ?? 3000);
|
|
}
|
|
bootstrap();
|