diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9ebe86e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM docker.io/library/node:22-alpine +LABEL org.opencontainers.image.source https://gianet.us/codyops/nucleus +COPY . /opt +WORKDIR /opt +RUN npm run build +CMD npm run start diff --git a/package.json b/package.json index 63cac06..40851b5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "CodyOps Nucleus", "main": "dist/index.js", "scripts": { - "build": "npx tsc", + "build": "npm i && npx tsc", "start": "node dist/main.js", "dev": "tsc-watch --noClear --onSuccess 'node ./dist/main.js'" }, diff --git a/src/main.ts b/src/main.ts index aeaefe1..2732179 100644 --- a/src/main.ts +++ b/src/main.ts @@ -35,6 +35,23 @@ app.post(`/${processor}`, jsonParser, async (req: Request, res: Response) => { res.sendStatus(201); }) -app.listen(port, () => { +const server = app.listen(port, () => { console.log('Serving function...'); -}); \ No newline at end of file +}); + +function shutdown(signal: string) { + console.log(`Received ${signal}. Shutting down gracefully...`); + + server.close(() => { + console.log('HTTP server closed.'); + process.exit(0); + }); + + setTimeout(() => { + console.error('Forced shutdown due to timeout.'); + process.exit(1); + }, 10000); +} + +process.on('SIGTERM', () => shutdown('SIGTERM')); +process.on('SIGINT', () => shutdown('SIGINT')); // Handles Ctrl+C