-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (59 loc) · 2.1 KB
/
server.js
File metadata and controls
73 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const path = require('path');
const http = require('http');
const { WebSocketServer } = require('ws');
const db = require('./lib/db');
const apiRouter = require('./lib/api');
const { createQueueManager } = require('./lib/queue');
const app = express();
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
// --- Middleware ---
app.use(express.json());
// --- State ---
// Current display mode tracked in memory for reconnecting clients
let currentMode = { action: 'clear' };
// --- WebSocket ---
const wss = new WebSocketServer({ server });
function broadcast(msg) {
const data = JSON.stringify(msg);
for (const client of wss.clients) {
if (client.readyState === 1) client.send(data);
}
}
function setCurrentMode(msg) {
currentMode = msg;
broadcast(msg);
}
wss.on('connection', (ws) => {
// Send current state on connect
const settings = db.getAllSettings();
ws.send(JSON.stringify({ action: 'sync', settings, currentMode }));
});
// --- Queue Manager ---
const queueManager = createQueueManager(db, setCurrentMode, broadcast);
// --- API ---
app.use('/api', apiRouter(db, setCurrentMode, broadcast, queueManager));
// --- Static files ---
// Serve original index.html at /
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Serve display page
app.get('/display', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'display', 'index.html'));
});
// Serve admin page
app.get('/admin', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin', 'index.html'));
});
// Static assets
app.use('/shared', express.static(path.join(__dirname, 'public', 'shared')));
app.use('/display', express.static(path.join(__dirname, 'public', 'display')));
app.use('/admin', express.static(path.join(__dirname, 'public', 'admin')));
server.listen(PORT, () => {
console.log(`Flippyboard server running on http://localhost:${PORT}`);
console.log(` Display: http://localhost:${PORT}/display`);
console.log(` Admin: http://localhost:${PORT}/admin`);
console.log(` Original: http://localhost:${PORT}/`);
});