85 lines
2.2 KiB
TypeScript
85 lines
2.2 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { Logger } from 'nestjs-pino';
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
import * as helmet from 'helmet';
|
|
import cookieParser from 'cookie-parser';
|
|
import { join } from 'node:path';
|
|
import { existsSync } from 'node:fs';
|
|
import { AppModule } from './app.module';
|
|
|
|
async function bootstrap(): Promise<void> {
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
bufferLogs: true,
|
|
});
|
|
|
|
const logger = app.get(Logger);
|
|
app.useLogger(logger);
|
|
|
|
app.use(
|
|
helmet.default({
|
|
contentSecurityPolicy: {
|
|
directives: {
|
|
defaultSrc: ["'self'"],
|
|
scriptSrc: ["'self'", 'https://cdn.jsdelivr.net'],
|
|
styleSrc: ["'self'", 'https://cdn.jsdelivr.net', "'unsafe-inline'"],
|
|
imgSrc: ["'self'", 'data:'],
|
|
fontSrc: ["'self'", 'https://cdn.jsdelivr.net'],
|
|
connectSrc: ["'self'"],
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
app.use(cookieParser());
|
|
|
|
app.enableCors({
|
|
origin: (process.env.CORS_ORIGINS ?? 'http://localhost:3010').split(','),
|
|
credentials: true,
|
|
});
|
|
|
|
const viewsPath = join(process.cwd(), 'views');
|
|
if (existsSync(viewsPath)) {
|
|
app.setBaseViewsDir(viewsPath);
|
|
app.setViewEngine('ejs');
|
|
}
|
|
|
|
const publicPath = join(process.cwd(), 'public');
|
|
if (existsSync(publicPath)) {
|
|
app.useStaticAssets(publicPath);
|
|
}
|
|
|
|
app.setGlobalPrefix('api', {
|
|
exclude: [
|
|
'/',
|
|
'/login',
|
|
'/register',
|
|
'/profile',
|
|
'/admin',
|
|
'/admin/users',
|
|
'/admin/clients',
|
|
'/metrics',
|
|
],
|
|
});
|
|
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
}),
|
|
);
|
|
|
|
const trustProxy = parseInt(process.env.TRUST_PROXY ?? '1', 10);
|
|
|
|
const instance: any = app.getHttpAdapter().getInstance();
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
instance.set('trust proxy', trustProxy);
|
|
|
|
const port = parseInt(process.env.PORT ?? '3010', 10);
|
|
const host = process.env.HOST ?? '0.0.0.0';
|
|
|
|
await app.listen(port, host);
|
|
logger.log(`SSO service listening on http://${host}:${port}`);
|
|
}
|
|
void bootstrap();
|