make rate-limiting and auto-restart optional via flag and modify readme

This commit is contained in:
schlagmichdoch 2023-01-11 14:21:54 +01:00
parent f12872bf5c
commit 57851902f9
6 changed files with 101 additions and 35 deletions

View file

@ -1,7 +1,7 @@
var process = require('process')
var crypto = require('crypto')
var {spawn} = require('child_process')
var net = require('net')
const process = require('process')
const crypto = require('crypto')
const {spawn} = require('child_process')
const net = require('net')
// Handle SIGINT
process.on('SIGINT', () => {
@ -29,41 +29,45 @@ process.on('unhandledRejection', (reason, promise) => {
console.log(reason)
})
process.on(
'uncaughtException',
() => {
process.once(
'exit',
() => spawn(
process.argv.shift(),
process.argv,
{
cwd: process.cwd(),
detached: true,
stdio: 'inherit'
}
)
);
process.exit();
}
);
if (process.argv.includes('--auto-restart')) {
process.on(
'uncaughtException',
() => {
process.once(
'exit',
() => spawn(
process.argv.shift(),
process.argv,
{
cwd: process.cwd(),
detached: true,
stdio: 'inherit'
}
)
);
process.exit();
}
);
}
const express = require('express');
const RateLimit = require('express-rate-limit');
const http = require('http');
const limiter = RateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 1000, // Limit each IP to 100 requests per `window` (here, per 5 minutes)
message: 'Too many requests from this IP Address, please try again after 5 minutes.',
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
const app = express();
const port = process.env.PORT || 3000;
app.use(limiter);
if (process.argv.includes('--rate-limit')) {
const limiter = RateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
max: 1000, // Limit each IP to 100 requests per `window` (here, per 5 minutes)
message: 'Too many requests from this IP Address, please try again after 5 minutes.',
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
app.use(limiter);
}
app.use(express.static('public'));