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();