From 5ebc03aaf7d2e612ed13895cde8a617820a08788 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 27 Nov 2024 08:40:57 +0100 Subject: [PATCH] feat(cli): update index template --- packages/cli/templates/init/src/index.ts.hbs | 40 ++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/cli/templates/init/src/index.ts.hbs b/packages/cli/templates/init/src/index.ts.hbs index a131cfcf0..650dccab2 100644 --- a/packages/cli/templates/init/src/index.ts.hbs +++ b/packages/cli/templates/init/src/index.ts.hbs @@ -2,17 +2,35 @@ import {$log} from "@tsed/logger"; import { {{platformSymbol}} } from "@tsed/platform-{{platform}}"; import {Server} from "./{{entryServer}}.js"; -async function bootstrap() { - try { - const platform = await {{platformSymbol}}.bootstrap(Server); - await platform.listen(); +const SIG_EVENTS = [ + "beforeExit", + "SIGHUP", + "SIGINT", + "SIGQUIT", + "SIGILL", + "SIGTRAP", + "SIGABRT", + "SIGBUS", + "SIGFPE", + "SIGUSR1", + "SIGSEGV", + "SIGUSR2", + "SIGTERM" +]; - process.on("SIGINT", () => { - platform.stop(); - }); - } catch (error) { - $log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack}); - } +try { + const platform = await {{platformSymbol}}.bootstrap(Server); + await platform.listen(); + + SIG_EVENTS.forEach((evt) => process.on(evt, close)); + + ["uncaughtException", "unhandledRejection"].forEach((evt) => + process.on(evt, async (error) => { + $log.error({event: "SERVER_" + evt.toUpperCase(), message: error.message, stack: error.stack}); + await stop(); + }) + ); +} catch (error) { + $log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack}); } -bootstrap();