From bcd18ab0afbe1c186766b4179102408e2703db4a Mon Sep 17 00:00:00 2001 From: Reginald Bondoc Date: Mon, 12 Dec 2022 19:11:44 +0100 Subject: [PATCH] Ignore linting healthcheck & exclue in rate-limiting --- .eslintignore | 3 +++ backend/healthcheck.js | 5 +---- backend/src/helpers/rateLimiter.ts | 33 +++++++++++++++--------------- backend/src/index.ts | 9 ++++++-- frontend/scripts/healthcheck.js | 5 +---- 5 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000000..7a3d615587 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +node_modules +built +healthcheck.js diff --git a/backend/healthcheck.js b/backend/healthcheck.js index 7b798eb0a8..8cb3dfcaaf 100644 --- a/backend/healthcheck.js +++ b/backend/healthcheck.js @@ -1,6 +1,3 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -/* eslint-disable no-console */ -/* eslint-disable no-undef */ const http = require('http'); const PORT = process.env.PORT || 4000; const options = { @@ -20,7 +17,7 @@ const healthCheck = http.request(options, (res) => { }); healthCheck.on('error', function (err) { - console.error(err); + console.error(`HEALTH CHECK ERROR: ${err}`); process.exit(1); }); diff --git a/backend/src/helpers/rateLimiter.ts b/backend/src/helpers/rateLimiter.ts index a0788246aa..6153369e38 100644 --- a/backend/src/helpers/rateLimiter.ts +++ b/backend/src/helpers/rateLimiter.ts @@ -2,34 +2,35 @@ import rateLimit from 'express-rate-limit'; // 300 requests per 15 minutes const apiLimiter = rateLimit({ - windowMs: 15 * 60 * 1000, - max: 400, - standardHeaders: true, - legacyHeaders: false + windowMs: 15 * 60 * 1000, + max: 400, + standardHeaders: true, + legacyHeaders: false, + skip: (request) => request.path === '/healthcheck' }); // 5 requests per hour const signupLimiter = rateLimit({ - windowMs: 60 * 60 * 1000, - max: 10, - standardHeaders: true, - legacyHeaders: false + windowMs: 60 * 60 * 1000, + max: 10, + standardHeaders: true, + legacyHeaders: false }); // 10 requests per hour const loginLimiter = rateLimit({ - windowMs: 60 * 60 * 1000, - max: 20, - standardHeaders: true, - legacyHeaders: false + windowMs: 60 * 60 * 1000, + max: 20, + standardHeaders: true, + legacyHeaders: false }); // 5 requests per hour const passwordLimiter = rateLimit({ - windowMs: 60 * 60 * 1000, - max: 10, - standardHeaders: true, - legacyHeaders: false + windowMs: 60 * 60 * 1000, + max: 10, + standardHeaders: true, + legacyHeaders: false }); export { apiLimiter, signupLimiter, loginLimiter, passwordLimiter }; diff --git a/backend/src/index.ts b/backend/src/index.ts index f9789c04d0..28e27cc9af 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -51,9 +51,10 @@ const connectWithRetry = () => { console.log(e); }, 5000); }); + return mongoose.connection; }; -connectWithRetry(); +const dbConnection = connectWithRetry(); app.enable('trust proxy'); app.use(cookieParser()); @@ -97,7 +98,11 @@ const server = http.createServer(app); const onSignal = () => { console.log('Server is starting clean-up'); return Promise.all([ - // your clean logic, like closing database connections + () => { + dbConnection.close(() => { + console.info('Database connection closed'); + }); + } ]); }; diff --git a/frontend/scripts/healthcheck.js b/frontend/scripts/healthcheck.js index b29d70f44d..30a964d9df 100644 --- a/frontend/scripts/healthcheck.js +++ b/frontend/scripts/healthcheck.js @@ -1,6 +1,3 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -/* eslint-disable no-console */ -/* eslint-disable no-undef */ const http = require('http'); const options = { host: 'localhost', @@ -19,7 +16,7 @@ const healthCheck = http.request(options, (res) => { }); healthCheck.on('error', function (err) { - console.error(err); + console.error(`HEALTH CHECK ERROR: ${err}`); process.exit(1); });