From c6500df4a7263620536e8f49412aefb3ae5b50b4 Mon Sep 17 00:00:00 2001 From: Czekaj Tom Date: Thu, 4 Jan 2024 04:42:07 +0100 Subject: [PATCH] :sparkles: Add swagger server address --- src/app.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index db242ff..e1006b3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -33,7 +33,7 @@ async function bootstrap(){ } } -function logServerStart(bindAddress: string, port: string | number, protocol: string){ +function getServerAddress(bindAddress: string, port: string | number, protocol: string){ if(bindAddress === "0.0.0.0"){ const ifaces = os.networkInterfaces(); Object.keys(ifaces).forEach(function(ifname){ @@ -49,12 +49,16 @@ function logServerStart(bindAddress: string, port: string | number, protocol: st }); }); } - console.log(`Server started on ${protocol}://${bindAddress}:${port}`); + return `${protocol}://${bindAddress}:${port}`; +} + +function logServerStart(bindAddress: string, port: string | number, protocol: string){ + console.log(`Server started on ${getServerAddress(bindAddress, port, protocol)}`); } async function startHttpServer(){ const httpApp = await NestFactory.create(AppModule , new FastifyAdapter({exposeHeadRoutes: true})); - await loadServer(httpApp); + await loadServer(httpApp, getServerAddress(process.env.BIND_ADDRESS, process.env.HTTP_PORT, "http")); await httpApp.listen(process.env.HTTP_PORT, process.env.BIND_ADDRESS); logServerStart(process.env.BIND_ADDRESS, process.env.HTTP_PORT, "http"); } @@ -65,12 +69,12 @@ async function startHttpsServer(){ cert: fs.readFileSync(process.env.SSL_CERT_FILE), }; const httpsApp = await NestFactory.create(AppModule, new FastifyAdapter({https: httpsOptions})); - await loadServer(httpsApp); + await loadServer(httpsApp, getServerAddress(process.env.BIND_ADDRESS, process.env.HTTP_PORT, "https")); await httpsApp.listen(process.env.HTTPS_PORT, process.env.BIND_ADDRESS); logServerStart(process.env.BIND_ADDRESS, process.env.HTTP_PORT, "https"); } -async function loadServer(server: NestFastifyApplication){ +async function loadServer(server: NestFastifyApplication, serverAddress: string){ // Config server.setGlobalPrefix(process.env.PREFIX); server.enableCors({ @@ -90,6 +94,7 @@ async function loadServer(server: NestFastifyApplication){ .setDescription("Documentation for the Phoenix API") .setVersion(process.env.npm_package_version) .addBearerAuth() + .addServer(serverAddress) .build(); const document = SwaggerModule.createDocument(server, config); const theme = new SwaggerTheme("v3");