-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
181 lines (158 loc) · 7.68 KB
/
Copy pathserver.js
File metadata and controls
181 lines (158 loc) · 7.68 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* server.js — LiveControl main server
*/
import express from "express";
import { createServer } from "http";
import { WebSocketServer } from "ws";
import path from "path";
import { fileURLToPath } from "url";
import { AbletonBridge } from "./ableton.js";
import { setSongMeta, saveOrder } from "./storage.js";
import os from "os";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.json());
// Malformed JSON bodies would otherwise throw inside express.json() and
// crash the request with an unhandled error / raw stack trace.
app.use((err, req, res, next) => {
if (err?.type === "entity.parse.failed") {
return res.status(400).json({ ok: false, error: "Malformed JSON body" });
}
next(err);
});
app.use(express.static(path.join(__dirname, "public")));
const server = createServer(app);
const wss = new WebSocketServer({ server });
function broadcast(data) {
const msg = JSON.stringify(data);
for (const client of wss.clients) {
if (client.readyState === 1) client.send(msg);
}
}
const bridge = new AbletonBridge((state) => {
broadcast({ type: "state", payload: state });
});
wss.on("connection", (ws) => {
console.log("[ws] Client connected");
ws.send(JSON.stringify({ type: "state", payload: bridge.state }));
ws.on("close", () => console.log("[ws] Client disconnected"));
ws.on("error", (e) => console.error("[ws] Error:", e.message));
});
/* ── Helpers ─────────────────────────────────────────────────────────────── */
function ok(res, data = {}) { res.json({ ok: true, ...data }); }
function fail(res, msg, status = 400) { res.status(status).json({ ok: false, error: msg }); }
async function wrap(res, fn) {
try { await fn(); }
catch (e) { console.error("[api]", e.message); fail(res, e.message, 500); }
}
/* ── State ───────────────────────────────────────────────────────────────── */
app.get("/api/state", (req, res) => res.json({ ok: true, payload: bridge.state }));
/* ── Transport ───────────────────────────────────────────────────────────── */
app.post("/api/play", (req, res) => wrap(res, async () => { await bridge.play(); ok(res); }));
app.post("/api/stop", (req, res) => wrap(res, async () => { await bridge.stop(); ok(res); }));
app.post("/api/continue", (req, res) => wrap(res, async () => { await bridge.continuePlaying(); ok(res); }));
app.post("/api/next-cue", (req, res) => wrap(res, async () => { await bridge.jumpToNextCue(); ok(res); }));
app.post("/api/prev-cue", (req, res) => wrap(res, async () => { await bridge.jumpToPrevCue(); ok(res); }));
app.post("/api/tempo", (req, res) => wrap(res, async () => {
const { bpm } = req.body;
const val = parseFloat(bpm);
if (!Number.isFinite(val)) return fail(res, "bpm required");
await bridge.setTempo(val);
ok(res);
}));
/* ── Setlist navigation ──────────────────────────────────────────────────── */
app.post("/api/jump/song/:songId", (req, res) =>
wrap(res, async () => { await bridge.jumpToSong(req.params.songId); ok(res); })
);
app.post("/api/jump/section/:songId/:sectionId", (req, res) =>
wrap(res, async () => {
await bridge.jumpToSection(req.params.songId, req.params.sectionId);
ok(res);
})
);
app.post("/api/reload", (req, res) =>
wrap(res, async () => { await bridge.reloadSetlist(); ok(res); })
);
/* ── Song metadata ───────────────────────────────────────────────────────── */
app.post("/api/song/:songId/notes", (req, res) =>
wrap(res, async () => {
const song = bridge.setlist.find((s) => s.id === req.params.songId);
if (!song) return fail(res, "Song not found");
const notes = req.body.notes ?? "";
await setSongMeta(song.name, { notes });
bridge.updateSongMeta(song.id, { notes });
ok(res);
})
);
app.post("/api/song/:songId/color", (req, res) =>
wrap(res, async () => {
const song = bridge.setlist.find((s) => s.id === req.params.songId);
if (!song) return fail(res, "Song not found");
const color = req.body.color ?? "";
await setSongMeta(song.name, { color });
bridge.updateSongMeta(song.id, { color });
ok(res);
})
);
app.post("/api/song/:songId/exclude", (req, res) =>
wrap(res, async () => {
const song = bridge.setlist.find((s) => s.id === req.params.songId);
if (!song) return fail(res, "Song not found");
const excluded = !!req.body.excluded;
await setSongMeta(song.name, { excluded });
bridge.updateSongMeta(song.id, { excluded });
ok(res);
})
);
app.post("/api/setlist/reorder", (req, res) =>
wrap(res, async () => {
const { order } = req.body;
if (!Array.isArray(order)) return fail(res, "order must be an array of IDs");
bridge.reorderSetlist(order);
await saveOrder(bridge.setlist.map((s) => s.name));
ok(res);
})
);
/* ── 404 + error handling ────────────────────────────────────────────────── */
app.use("/api", (req, res) => fail(res, "Not found", 404));
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
console.error("[server] Unhandled error:", err);
if (res.headersSent) return next(err);
fail(res, "Internal server error", 500);
});
/* ── Start ───────────────────────────────────────────────────────────────── */
server.listen(PORT, "0.0.0.0", async () => {
const interfaces = os.networkInterfaces();
let localIP = "localhost";
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === "IPv4" && !iface.internal) { localIP = iface.address; break; }
}
}
console.log("\n╔════════════════════════════════════════╗");
console.log("║ LiveControl is running ║");
console.log("╠════════════════════════════════════════╣");
console.log(`║ Local: http://localhost:${PORT} ║`);
console.log(`║ Network: http://${localIP}:${PORT} ║`);
console.log("╚════════════════════════════════════════╝\n");
try {
await bridge.start();
} catch (e) {
console.error("[server] Failed to start Ableton bridge:", e.message);
}
});
/* ── Graceful shutdown ───────────────────────────────────────────────────── */
function shutdown(signal) {
console.log(`\n[server] ${signal} received, shutting down…`);
for (const client of wss.clients) client.close();
server.close(() => {
console.log("[server] Closed. Bye!");
process.exit(0);
});
// Force-exit if something keeps the event loop alive
setTimeout(() => process.exit(0), 2000).unref();
}
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));