Test WS
This commit is contained in:
parent
71363b6d11
commit
b0a1cc6154
1
index.js
1
index.js
|
@ -45,6 +45,7 @@ global.comparePassword = async function(password, hash) {
|
|||
};
|
||||
|
||||
global.checkACL = function(req, perm) {
|
||||
if (!req.session.user) return false;
|
||||
const perms = req.session.user.perms ? JSON.parse(req.session.user.perms) : [];
|
||||
if (perms.includes('*') || perms.includes(perm)) {
|
||||
return true;
|
||||
|
|
31
routes/event-logs.js
Normal file
31
routes/event-logs.js
Normal 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
72
views/event-logs.ejs
Normal 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>
|
Loading…
Reference in a new issue