diff --git a/package-lock.json b/package-lock.json index 6574e87..3ff3a6a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,8 @@ "express-validator": "^7.3.1", "mongodb": "^7.0.0", "morgan": "^1.10.1", - "nodemon": "^3.1.11" + "nodemon": "^3.1.11", + "ws": "^8.18.3" } }, "node_modules/@mongodb-js/saslprep": { @@ -1475,6 +1476,27 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "license": "ISC" + }, + "node_modules/ws": { + "version": "8.18.3", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.3.tgz", + "integrity": "sha512-PEIGCY5tSlUt50cqyMXfCzX+oOPqN0vuGqWzbcJ2xvnkzkq46oOpz7dQaTDBdfICb4N14+GARUDw2XV2N4tvzg==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } } } } diff --git a/package.json b/package.json index eb8f3b3..07ed8ea 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "express-validator": "^7.3.1", "mongodb": "^7.0.0", "morgan": "^1.10.1", - "nodemon": "^3.1.11" + "nodemon": "^3.1.11", + "ws": "^8.18.3" } } diff --git a/server.js b/server.js index 9c73256..d92c1cb 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,9 @@ const express = require('express'); const path = require('path'); const morgan = require('morgan'); const bodyParser = require('body-parser'); +const http = require('http'); +const WebSocket = require('ws'); +const { spawn } = require('child_process'); const indexRouter = require('./routes/index'); @@ -24,7 +27,6 @@ app.use(bodyParser.json()); // skip: (req) => req.url.startsWith('/assets') || req.url.startsWith('/favicon.ico') // })); - // Routes app.use('/', indexRouter); @@ -37,6 +39,29 @@ app.use((err, req, res, next) => { }); }); -app.listen(PORT, () => +// ----------------------------- +// Wrap Express in HTTP + WS +// ----------------------------- +const server = http.createServer(app); +const wss = new WebSocket.Server({ server }); + +// When a client connects, stream PM2 logs +wss.on('connection', ws => { + const pm2Logs = spawn('pm2', ['logs', '--json']); + + pm2Logs.stdout.on('data', data => { + ws.send(data.toString()); + }); + + pm2Logs.stderr.on('data', data => { + ws.send('ERROR: ' + data.toString()); + }); + + ws.on('close', () => { + pm2Logs.kill(); + }); +}); + +server.listen(PORT, () => console.log(`Proxer-Downloader running on http://localhost:${PORT}`) ); diff --git a/views/status.ejs b/views/status.ejs index d70ffa7..19e224f 100644 --- a/views/status.ejs +++ b/views/status.ejs @@ -127,10 +127,27 @@ + + + +