From bdab23adbee95ca28589a7579993333bd03c3c53 Mon Sep 17 00:00:00 2001 From: Type-Style Date: Wed, 7 Feb 2024 14:38:01 +0100 Subject: [PATCH] [Task] #34 installed and integrated tooBusy to send 503 when load is high --- package-lock.json | 18 +++++++++++++++++- package.json | 4 +++- src/app.ts | 40 ++++++++++++++++++++++++---------------- src/scripts/logger.ts | 1 - 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4829c14..d4c3d8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "helmet": "^7.1.0", "hpp": "^0.2.3", "module-alias": "^2.2.3", - "raw-body": "^2.5.2" + "raw-body": "^2.5.2", + "toobusy-js": "^0.5.1" }, "devDependencies": { "@jest/globals": "^29.7.0", @@ -27,6 +28,7 @@ "@types/hpp": "^0.2.5", "@types/jest": "^29.5.11", "@types/node": "^20.10.6", + "@types/toobusy-js": "^0.5.4", "@typescript-eslint/eslint-plugin": "^6.18.1", "@typescript-eslint/parser": "^6.18.1", "axios": "^1.6.5", @@ -1692,6 +1694,12 @@ "integrity": "sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==", "dev": true }, + "node_modules/@types/toobusy-js": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/@types/toobusy-js/-/toobusy-js-0.5.4.tgz", + "integrity": "sha512-hsKMbYiaL3ZWx7B3FYyN0rEJexw7I1HgKbNToX3ZZJv6373to954wlA7zrXR3/XoVwZnFwWqFguBs91sNzJGKQ==", + "dev": true + }, "node_modules/@types/yargs": { "version": "17.0.32", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz", @@ -6106,6 +6114,14 @@ "node": ">=0.6" } }, + "node_modules/toobusy-js": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/toobusy-js/-/toobusy-js-0.5.1.tgz", + "integrity": "sha512-GiCux/c8G2TV0FTDgtxnXOxmSAndaI/9b1YxT14CqyeBDtTZAcJLx9KlXT3qECi8D0XCc78T4sN/7gWtjRyCaA==", + "engines": { + "node": ">=0.9.1" + } + }, "node_modules/touch": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/touch/-/touch-3.1.0.tgz", diff --git a/package.json b/package.json index 4c528b7..f78fb59 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "@types/hpp": "^0.2.5", "@types/jest": "^29.5.11", "@types/node": "^20.10.6", + "@types/toobusy-js": "^0.5.4", "@typescript-eslint/eslint-plugin": "^6.18.1", "@typescript-eslint/parser": "^6.18.1", "axios": "^1.6.5", @@ -44,7 +45,8 @@ "helmet": "^7.1.0", "hpp": "^0.2.3", "module-alias": "^2.2.3", - "raw-body": "^2.5.2" + "raw-body": "^2.5.2", + "toobusy-js": "^0.5.1" }, "_moduleAliases": { "@src": "dist" diff --git a/src/app.ts b/src/app.ts index 19a6670..8074341 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,6 +1,7 @@ require('module-alias/register'); import { config } from 'dotenv'; import express from 'express'; +import toobusy from 'toobusy-js'; import compression from 'compression'; import helmet from 'helmet'; import hpp from 'hpp'; @@ -13,21 +14,26 @@ import path from 'path'; import logger from '@src/scripts/logger'; // configurations -config(); +config(); // dotenv + const app = express(); -app.use( - helmet({ - contentSecurityPolicy: { - directives: { - "default-src": "'self'", - "img-src": "*" - } +app.use(helmet({ + contentSecurityPolicy: { + directives: { + "default-src": "'self'", + "img-src": "*" } - }) -); + } +})); +app.use((req, res, next) => { + if (toobusy()) { + res.status(503).send("I'm busy right now, sorry."); + // todo add headers retry after and no cache + } else { next(); } +}); +app.use(cache); app.use(compression()) app.use(hpp()); -app.use(cache); app.use(function (req, res, next) { if (!['POST', 'PUT', 'DELETE'].includes(req.method)) { @@ -48,9 +54,11 @@ app.get('/', (req, res) => { res.send('Hello World, via TypeScript and Node.js!'); }); -app.get('/test', (req, res) => { - res.send('Hello Test!'); - process.exit(); +app.get('/test', function (req, res) { + // processing the request requires some work! + let i = 0; + while (i < 1e10) i++; + res.send("I counted to " + i); }); @@ -60,7 +68,7 @@ app.use('/read', readRouter); // use httpdocs as static folder app.use('/', express.static(path.join(__dirname, 'httpdocs'), { extensions: ['html', 'txt', "pdf"], - index: ["start.html", "start.txt"] , + index: ["start.html", "start.txt"], })); // error handling @@ -77,7 +85,7 @@ const server = app.listen(80, () => { process.on(signal, () => { function logAndExit() { // calling .shutdown allows your process to exit normally - // toobusy.shutdown(); + toobusy.shutdown(); logger.log(`Server shutdown on signal: ${signal} //localhost:80`, true); process.exit(); } diff --git a/src/scripts/logger.ts b/src/scripts/logger.ts index 07b43ac..8de1742 100644 --- a/src/scripts/logger.ts +++ b/src/scripts/logger.ts @@ -9,7 +9,6 @@ const logPath = path.resolve(dirPath, 'start.txt'); if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); - console.log("path created") } // const logPath = path.resolve(__dirname, '../httpdocs/log', 'start.txt');