added pm2 status page and updated others
This commit is contained in:
1 parent
39c2a3f1ef
commit
c07a08dfc0
9 files changed
+1017
-223
No files matched your search
@@ -1,8 +1,7 @@
|
||||
// server.js
|
||||
// server.js (Improved Version B)
|
||||
require('dotenv').config();
|
||||
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');
|
||||
@@ -22,46 +21,126 @@ app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// Morgan: log only non-static requests
|
||||
// app.use(morgan(':method :url :status :res[content-length] - :response-time ms', {
|
||||
// skip: (req) => req.url.startsWith('/assets') || req.url.startsWith('/favicon.ico')
|
||||
// }));
|
||||
|
||||
// Routes
|
||||
app.use('/', indexRouter);
|
||||
|
||||
// Error handler
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err);
|
||||
res.status(500).render('error', {
|
||||
message: 'Internal Server Error',
|
||||
detail: err.message
|
||||
});
|
||||
console.error(err);
|
||||
res.status(500).render('error', {
|
||||
message: 'Internal Server Error',
|
||||
detail: err.message
|
||||
});
|
||||
});
|
||||
|
||||
// -----------------------------
|
||||
// Wrap Express in HTTP + WS
|
||||
// HTTP + WebSocket server
|
||||
// -----------------------------
|
||||
const server = http.createServer(app);
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
// When a client connects, stream PM2 logs
|
||||
// Heartbeat for detecting dead connections
|
||||
function heartbeat() {
|
||||
this.isAlive = true;
|
||||
}
|
||||
|
||||
wss.on('connection', ws => {
|
||||
const pm2Logs = spawn('pm2', ['logs', '--json']);
|
||||
ws.isAlive = true;
|
||||
ws.on('pong', heartbeat);
|
||||
|
||||
pm2Logs.stdout.on('data', data => {
|
||||
ws.send(data.toString());
|
||||
});
|
||||
let child = null;
|
||||
|
||||
pm2Logs.stderr.on('data', data => {
|
||||
ws.send('ERROR: ' + data.toString());
|
||||
});
|
||||
ws.send(JSON.stringify({ type: 'connected', message: 'ws ready' }));
|
||||
|
||||
ws.on('close', () => {
|
||||
pm2Logs.kill();
|
||||
});
|
||||
ws.on('message', msg => {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(msg.toString());
|
||||
} catch {
|
||||
ws.send(JSON.stringify({ type: 'error', error: 'Invalid JSON' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// START LOGS
|
||||
// -----------------------------
|
||||
if (payload.type === 'logs') {
|
||||
const name = payload.name || null;
|
||||
|
||||
// Clean previous child
|
||||
if (child) {
|
||||
try { child.kill(); } catch {}
|
||||
child = null;
|
||||
}
|
||||
|
||||
// Correct PM2 argument ordering
|
||||
const args = ['logs', '--raw', '--lines', '200'];
|
||||
if (name) args.splice(1, 0, name);
|
||||
|
||||
try {
|
||||
child = spawn('pm2', args);
|
||||
|
||||
child.stdout.on('data', data => {
|
||||
ws.send(JSON.stringify({ type: 'stdout', data: data.toString() }));
|
||||
});
|
||||
|
||||
child.stderr.on('data', data => {
|
||||
ws.send(JSON.stringify({ type: 'stderr', data: data.toString() }));
|
||||
});
|
||||
|
||||
child.on('close', code => {
|
||||
ws.send(JSON.stringify({ type: 'close', code }));
|
||||
child = null;
|
||||
});
|
||||
|
||||
child.on('error', err => {
|
||||
ws.send(JSON.stringify({ type: 'error', error: err.message }));
|
||||
child = null;
|
||||
});
|
||||
|
||||
ws.send(JSON.stringify({ type: 'info', message: `started logs for ${name || 'all'}` }));
|
||||
|
||||
} catch (err) {
|
||||
ws.send(JSON.stringify({ type: 'error', error: err.message }));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// STOP LOGS
|
||||
// -----------------------------
|
||||
if (payload.type === 'stop-logs') {
|
||||
if (child) {
|
||||
try { child.kill(); } catch {}
|
||||
child = null;
|
||||
}
|
||||
ws.send(JSON.stringify({ type: 'info', message: 'logs stopped' }));
|
||||
return;
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({ type: 'error', error: 'Unknown type' }));
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
if (child) {
|
||||
try { child.kill(); } catch {}
|
||||
child = null;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Ping-pong interval every 30s
|
||||
setInterval(() => {
|
||||
wss.clients.forEach(ws => {
|
||||
if (!ws.isAlive) return ws.terminate();
|
||||
|
||||
ws.isAlive = false;
|
||||
ws.ping(); // triggers 'pong'
|
||||
});
|
||||
}, 30000);
|
||||
|
||||
// Start server
|
||||
server.listen(PORT, () =>
|
||||
console.log(`Proxer-Downloader running on http://localhost:${PORT}`)
|
||||
console.log(`Proxer-Downloader running on http://localhost:${PORT}`)
|
||||
);
|
||||
Reference in new issue
Block a user