-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (50 loc) · 1.7 KB
/
server.js
File metadata and controls
61 lines (50 loc) · 1.7 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
import 'dotenv/config';
import express from 'express';
import fetch from 'node-fetch';
const TOKEN = process.env.COMREX_API_TOKEN;
const PORT = process.env.PORT || 3000;
const UPSTREAM_URL = 'https://switchboard.comrex.com/switchboard/api/v1/units/access';
const CACHE_TTL_MS = 29_000;
if (!TOKEN) {
console.error('ERROR: COMREX_API_TOKEN is not set. Add it to your .env file.');
process.exit(1);
}
const app = express();
app.use(express.static('public'));
// Server-side response cache to coalesce multi-tab polls
let cache = { data: null, fetchedAt: 0 };
app.get('/api/units', async (req, res) => {
const now = Date.now();
if (cache.data && now - cache.fetchedAt < CACHE_TTL_MS) {
return res.json(cache.data);
}
let upstreamRes;
try {
upstreamRes = await fetch(UPSTREAM_URL, {
headers: { Authorization: `Bearer ${TOKEN}` },
});
} catch (err) {
console.error('Network error reaching Comrex API:', err.message);
return res.status(503).json({ error: 'Unable to reach Comrex API', status: 503 });
}
if (!upstreamRes.ok) {
console.error(`Comrex API returned ${upstreamRes.status} ${upstreamRes.statusText}`);
return res.status(502).json({
error: `Upstream API error (${upstreamRes.status})`,
upstreamStatus: upstreamRes.status,
status: 502,
});
}
let body;
try {
body = await upstreamRes.json();
} catch (err) {
console.error('Failed to parse Comrex API response:', err.message);
return res.status(502).json({ error: 'Invalid response from upstream API', status: 502 });
}
cache = { data: body, fetchedAt: now };
res.json(body);
});
app.listen(PORT, () => {
console.log(`Comrex Dashboard running at http://localhost:${PORT}`);
});