This commit is contained in:
Christopher Cookman 2025-08-31 22:54:11 -06:00
parent 71363b6d11
commit b0a1cc6154
3 changed files with 104 additions and 0 deletions

View file

@ -45,6 +45,7 @@ global.comparePassword = async function(password, hash) {
}; };
global.checkACL = function(req, perm) { global.checkACL = function(req, perm) {
if (!req.session.user) return false;
const perms = req.session.user.perms ? JSON.parse(req.session.user.perms) : []; const perms = req.session.user.perms ? JSON.parse(req.session.user.perms) : [];
if (perms.includes('*') || perms.includes(perm)) { if (perms.includes('*') || perms.includes(perm)) {
return true; return true;

31
routes/event-logs.js Normal file
View file

@ -0,0 +1,31 @@
const express = require('express');
const ews = require('express-ws');
const db = global.db;
const router = express.Router();
router.use(ews(router));
// GET /login
router.get('/', async (req, res) => {
const logs = await db.query('SELECT * FROM Events ORDER BY EventIndex DESC LIMIT 100');
res.render('event-logs', { logs, user: req.session.user });
});
router.ws('/', (ws, req) => {
if (!req.session.user) {
ws.send(JSON.stringify({ error: 'Not authenticated' }))
ws.close();
return;
}
if (global.checkACL(req, 'eventLog') == false) {
ws.send(JSON.stringify({ error: 'Not authorized' }))
ws.close();
return;
}
global.dbEvent.on('event', (event) => {
ws.send(JSON.stringify(event));
});
});
module.exports = router;

72
views/event-logs.ejs Normal file
View file

@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Log Viewer</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f7f7f7;
margin: 0;
padding: 0;
}
.container {
max-width: 900px;
margin: 40px auto;
background: #fff;
padding: 32px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
h1 {
text-align: center;
margin-bottom: 32px;
color: #333;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 16px;
}
th, td {
padding: 12px 8px;
border-bottom: 1px solid #e0e0e0;
text-align: left;
}
th {
background: #f0f0f0;
color: #444;
}
tr:hover {
background: #f9f9f9;
}
</style>
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//${location.host}${location.pathname}`;
const ws = new WebSocket(wsUrl);
ws.onmessage = function(event) {
// Handle incoming event log messages
// Example: append to a table or display in the UI
console.log('Event received:', event);
};
ws.onopen = function() {
console.log('WebSocket connection established');
};
ws.onclose = function() {
console.log('WebSocket connection closed');
};
ws.onerror = function(error) {
console.error('WebSocket error:', error);
};
</script>
</head>
<body>
</body>
</html>