Skip to content

Commit c2653de

Browse files
committed
🎨 Improve app loading and env variables usage
1 parent a23ee72 commit c2653de

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/app.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,29 @@ import {Logger} from "@nestjs/common";
99
import * as process from "process";
1010
import * as dotenv from "dotenv";
1111
import {FastifyListenOptions} from "fastify/types/instance";
12+
import {RawServerDefault} from "fastify";
1213

1314
dotenv.config();
1415

1516
const logger: Logger = new Logger("App");
1617

1718
async function bootstrap(){
18-
const app = await NestFactory.create<NestFastifyApplication>(
19+
const app: NestFastifyApplication<RawServerDefault> = await NestFactory.create<NestFastifyApplication>(
1920
AppModule,
2021
new FastifyAdapter({exposeHeadRoutes: true}),
2122
);
2223
await loadServer(app);
23-
const port = process.env.PORT || 4000;
2424
// @ts-ignore
2525
await app.listen({
26-
port: port,
26+
port: process.env.PORT || 4000,
2727
host: "0.0.0.0",
2828
} as FastifyListenOptions);
2929
app.enableShutdownHooks();
30-
logger.log(`Listening on http://0.0.0.0:${port}`);
3130
}
3231

3332
async function loadServer(server: NestFastifyApplication){
3433
// Config
35-
server.setGlobalPrefix(process.env.PREFIX);
34+
server.setGlobalPrefix(process.env.PREFIX || "");
3635
server.enableCors({
3736
origin: "*",
3837
});
@@ -56,7 +55,7 @@ async function loadServer(server: NestFastifyApplication){
5655
const document = SwaggerModule.createDocument(server, config);
5756
const theme = new SwaggerTheme();
5857
const customCss = theme.getBuffer(SwaggerThemeNameEnum.DARK);
59-
SwaggerModule.setup("", server, document, {
58+
SwaggerModule.setup(process.env.PREFIX || "", server, document, {
6059
swaggerOptions: {
6160
filter: true,
6261
displayRequestDuration: true,
@@ -69,4 +68,6 @@ async function loadServer(server: NestFastifyApplication){
6968
});
7069
}
7170

72-
bootstrap();
71+
bootstrap().then(() => {
72+
logger.log(`Listening on http://0.0.0.0:${process.env.PORT || 4000}`);
73+
});

0 commit comments

Comments
 (0)