2024-04-04 23:18:00 +02:00
|
|
|
import {
|
|
|
|
ClassSerializerInterceptor,
|
|
|
|
Logger,
|
2024-09-27 16:02:49 +02:00
|
|
|
LogLevel,
|
2024-04-04 23:18:00 +02:00
|
|
|
ValidationPipe,
|
|
|
|
} from "@nestjs/common";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { NestFactory, Reflector } from "@nestjs/core";
|
2022-10-24 12:11:10 +02:00
|
|
|
import { NestExpressApplication } from "@nestjs/platform-express";
|
2023-02-07 11:23:43 +01:00
|
|
|
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
2023-01-09 11:43:48 +01:00
|
|
|
import * as bodyParser from "body-parser";
|
2023-01-04 11:54:28 +01:00
|
|
|
import * as cookieParser from "cookie-parser";
|
2024-04-05 11:31:43 +02:00
|
|
|
import { NextFunction, Request, Response } from "express";
|
2022-10-09 22:30:32 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import { AppModule } from "./app.module";
|
2024-04-04 20:54:21 +02:00
|
|
|
import { ConfigService } from "./config/config.service";
|
2024-09-27 16:03:53 +02:00
|
|
|
import {
|
|
|
|
DATA_DIRECTORY,
|
|
|
|
LOG_LEVEL_AVAILABLE,
|
|
|
|
LOG_LEVEL_DEFAULT,
|
|
|
|
LOG_LEVEL_ENV,
|
|
|
|
} from "./constants";
|
2024-09-27 16:02:49 +02:00
|
|
|
|
|
|
|
function generateNestJsLogLevels(): LogLevel[] {
|
|
|
|
if (LOG_LEVEL_ENV) {
|
|
|
|
const levelIndex = LOG_LEVEL_AVAILABLE.indexOf(LOG_LEVEL_ENV as any);
|
|
|
|
if (levelIndex === -1) {
|
|
|
|
throw new Error(`log level ${LOG_LEVEL_ENV} unknown`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LOG_LEVEL_AVAILABLE.slice(levelIndex, LOG_LEVEL_AVAILABLE.length);
|
|
|
|
} else {
|
|
|
|
const levelIndex = LOG_LEVEL_AVAILABLE.indexOf(LOG_LEVEL_DEFAULT);
|
|
|
|
return LOG_LEVEL_AVAILABLE.slice(levelIndex, LOG_LEVEL_AVAILABLE.length);
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 12:11:10 +02:00
|
|
|
|
2022-10-09 22:30:32 +02:00
|
|
|
async function bootstrap() {
|
2024-09-27 16:02:49 +02:00
|
|
|
const logLevels = generateNestJsLogLevels();
|
|
|
|
Logger.log(`Showing ${logLevels.join(", ")} messages`);
|
|
|
|
|
|
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
2024-09-27 16:03:53 +02:00
|
|
|
logger: logLevels,
|
2024-09-27 16:02:49 +02:00
|
|
|
});
|
|
|
|
|
2022-12-05 16:54:15 +01:00
|
|
|
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
|
2022-10-09 22:30:32 +02:00
|
|
|
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
|
|
|
|
|
2024-04-04 20:54:21 +02:00
|
|
|
const config = app.get<ConfigService>(ConfigService);
|
2024-04-04 23:18:00 +02:00
|
|
|
|
2024-04-05 11:31:43 +02:00
|
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
|
|
const chunkSize = config.get("share.chunkSize");
|
2024-04-04 20:54:21 +02:00
|
|
|
bodyParser.raw({
|
|
|
|
type: "application/octet-stream",
|
2024-04-05 11:31:43 +02:00
|
|
|
limit: `${chunkSize}B`,
|
|
|
|
})(req, res, next);
|
|
|
|
});
|
2024-04-04 20:54:21 +02:00
|
|
|
|
2023-01-04 11:54:28 +01:00
|
|
|
app.use(cookieParser());
|
2022-10-24 12:11:10 +02:00
|
|
|
app.set("trust proxy", true);
|
|
|
|
|
2023-05-05 11:37:02 +02:00
|
|
|
await fs.promises.mkdir(`${DATA_DIRECTORY}/uploads/_temp`, {
|
|
|
|
recursive: true,
|
|
|
|
});
|
2022-10-09 22:30:32 +02:00
|
|
|
|
|
|
|
app.setGlobalPrefix("api");
|
2023-02-07 11:23:43 +01:00
|
|
|
|
|
|
|
// Setup Swagger in development mode
|
|
|
|
if (process.env.NODE_ENV == "development") {
|
|
|
|
const config = new DocumentBuilder()
|
|
|
|
.setTitle("Pingvin Share API")
|
|
|
|
.setVersion("1.0")
|
|
|
|
.build();
|
|
|
|
const document = SwaggerModule.createDocument(app, config);
|
|
|
|
SwaggerModule.setup("api/swagger", app, document);
|
|
|
|
}
|
|
|
|
|
2023-05-05 11:37:02 +02:00
|
|
|
await app.listen(parseInt(process.env.PORT) || 8080);
|
2024-04-04 23:18:00 +02:00
|
|
|
|
|
|
|
const logger = new Logger("UnhandledAsyncError");
|
|
|
|
process.on("unhandledRejection", (e) => logger.error(e));
|
2022-10-09 22:30:32 +02:00
|
|
|
}
|
|
|
|
bootstrap();
|