45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import "reflect-metadata";
|
|
import { NestFactory } from "@nestjs/core";
|
|
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
|
import { FastifyAdapter } from "@nestjs/platform-fastify";
|
|
import multipart from "@fastify/multipart";
|
|
import fastifyStatic from "@fastify/static";
|
|
import path from "node:path";
|
|
import { AppModule, configureApp } from "./app.module.js";
|
|
import { assertProductionSecret } from "./runtime-config.js";
|
|
|
|
export async function createApp(): Promise<NestFastifyApplication> {
|
|
assertProductionSecret(process.env.SESSION_SECRET);
|
|
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
|
|
app
|
|
.getHttpAdapter()
|
|
.getInstance()
|
|
.addContentTypeParser(
|
|
["image/jpeg", "image/png", "image/webp", "image/avif"],
|
|
{ parseAs: "buffer", bodyLimit: 10 * 1024 * 1024 },
|
|
(_request: unknown, body: Buffer, done: (error: Error | null, body?: Buffer) => void) =>
|
|
done(null, body),
|
|
);
|
|
await app.register(multipart);
|
|
if (process.env.LOCAL_MEDIA_ROOT) {
|
|
await app.register(fastifyStatic, {
|
|
root: path.resolve(process.env.LOCAL_MEDIA_ROOT),
|
|
prefix: "/media/",
|
|
decorateReply: false,
|
|
});
|
|
}
|
|
await configureApp(app);
|
|
return app;
|
|
}
|
|
|
|
if (process.env.NODE_ENV !== "test") {
|
|
process.on("unhandledRejection", (reason) => {
|
|
console.error("[api] unhandled rejection", reason);
|
|
});
|
|
process.on("uncaughtException", (error) => {
|
|
console.error("[api] uncaught exception", error);
|
|
});
|
|
const app = await createApp();
|
|
await app.listen({ port: Number(process.env.API_PORT ?? 3001), host: "0.0.0.0" });
|
|
}
|