mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 07:05:05 -04:00
Modularize server into multiple files
This commit is contained in:
parent
d9270a5560
commit
cb72edef20
8 changed files with 923 additions and 901 deletions
73
server/server.js
Normal file
73
server/server.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
import express from "express";
|
||||
import RateLimit from "express-rate-limit";
|
||||
import {fileURLToPath} from "url";
|
||||
import path, {dirname} from "path";
|
||||
import http from "http";
|
||||
|
||||
export default class PairDropServer {
|
||||
|
||||
constructor(conf) {
|
||||
const app = express();
|
||||
|
||||
if (conf.rateLimit) {
|
||||
const limiter = RateLimit({
|
||||
windowMs: 5 * 60 * 1000, // 5 minutes
|
||||
max: 1000, // Limit each IP to 1000 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);
|
||||
// ensure correct client ip and not the ip of the reverse proxy is used for rate limiting
|
||||
// see https://express-rate-limit.mintlify.app/guides/troubleshooting-proxy-issues
|
||||
|
||||
app.set('trust proxy', conf.rateLimit);
|
||||
|
||||
if (!conf.debugMode) {
|
||||
console.log("Use DEBUG_MODE=true to find correct number for RATE_LIMIT.");
|
||||
}
|
||||
}
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
const publicPathAbs = conf.wsFallback
|
||||
? path.join(__dirname, '../public_included_ws_fallback')
|
||||
: path.join(__dirname, '../public');
|
||||
|
||||
app.use(express.static(publicPathAbs));
|
||||
|
||||
app.use((req, res) => {
|
||||
if (conf.debugMode && conf.rateLimit && req.path === "/ip") {
|
||||
console.debug("\n");
|
||||
console.debug("----DEBUG RATE_LIMIT----")
|
||||
console.debug("To find out the correct value for RATE_LIMIT go to '/ip' and ensure the returned IP-address is the IP-address of your client.")
|
||||
console.debug("See https://github.com/express-rate-limit/express-rate-limit#troubleshooting-proxy-issues for more info")
|
||||
res.send(req.ip);
|
||||
}
|
||||
|
||||
res.redirect(301, '/');
|
||||
});
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile('index.html');
|
||||
console.log(`Serving client files from:\n${publicPathAbs}`)
|
||||
});
|
||||
|
||||
const hostname = conf.localhostOnly ? '127.0.0.1' : null;
|
||||
const server = http.createServer(app);
|
||||
|
||||
server.listen(conf.port, hostname);
|
||||
|
||||
server.on('error', (err) => {
|
||||
if (err.code === 'EADDRINUSE') {
|
||||
console.error(err);
|
||||
console.info("Error EADDRINUSE received, exiting process without restarting process...");
|
||||
process.exit(1)
|
||||
}
|
||||
});
|
||||
|
||||
this.server = server
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue