Skip to content

Commit

Permalink
refactor(server): use config service in server entry file (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Feb 7, 2024
1 parent 197fe5d commit b72be42
Show file tree
Hide file tree
Showing 3 changed files with 3,948 additions and 4,031 deletions.
10 changes: 5 additions & 5 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Logger, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { FastifyAdapter } from "@nestjs/platform-fastify";
import type { NestFastifyApplication } from "@nestjs/platform-fastify";

import { DEFAULT_APP_HOST, DEFAULT_APP_PORT } from "@/modules/config/env/constants/env.constant";

import { FASTIFY_SERVER_DEFAULT_OPTIONS } from "@/server/constants/server.constant";
import { createSwaggerDocument } from "@/server/swagger/swagger";

Expand All @@ -14,16 +13,17 @@ import { AppModule } from "@/app.module";

async function bootstrap(): Promise<NestFastifyApplication> {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(FASTIFY_SERVER_DEFAULT_OPTIONS));
app.enableCors({ origin: process.env.CORS_ORIGIN ?? "*" });
const configService = app.get<ConfigService>(ConfigService);
app.enableCors({ origin: configService.getOrThrow<string>("CORS_ORIGIN") });
app.useGlobalPipes(new ValidationPipe(DEFAULT_VALIDATION_PIPE_OPTIONS));
const documentationPath = "docs";
createSwaggerDocument(documentationPath, app);
app.useStaticAssets({
root: `${process.cwd()}/public`,
prefix: "/public/",
});
const host = process.env.HOST ?? DEFAULT_APP_HOST;
const port = process.env.PORT ?? DEFAULT_APP_PORT;
const host = configService.getOrThrow<string>("HOST");
const port = configService.getOrThrow<number>("PORT");
await app.listen(port, host);
const appUrl = await app.getUrl();
Logger.log(`馃惡 App is available at ${appUrl}`, "NestApplication");
Expand Down

0 comments on commit b72be42

Please sign in to comment.