Files
persionalBrand/apps/api/src/main.ts
T

43 lines
1.5 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";
export async function createApp(): Promise<NestFastifyApplication> {
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" });
}