Files
QueueCube/src/server.ts

102 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-02-15 00:37:32 -08:00
import express from "express";
import expressWs from "express-ws";
import { MediaPlayer } from "./MediaPlayer";
interface PlaylistAppendRequest {
url: string;
}
const app = express();
expressWs(app);
app.use(express.json());
2025-02-15 01:26:10 -08:00
const apiRouter = express.Router();
2025-02-15 00:37:32 -08:00
const mediaPlayer = new MediaPlayer();
2025-02-15 01:26:10 -08:00
apiRouter.get("/playlist", async (req, res) => {
2025-02-15 00:37:32 -08:00
const playlist = await mediaPlayer.getPlaylist();
res.send(playlist);
});
2025-02-15 01:26:10 -08:00
apiRouter.post("/playlist", async (req, res) => {
2025-02-15 00:37:32 -08:00
try {
const { url } = req.body as PlaylistAppendRequest;
await mediaPlayer.append(url);
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
2025-02-15 01:26:10 -08:00
apiRouter.delete("/playlist/:index", async (req, res) => {
2025-02-15 00:37:32 -08:00
const { index } = req.params as { index: string };
await mediaPlayer.deletePlaylistItem(parseInt(index));
res.send(JSON.stringify({ success: true }));
});
2025-02-15 01:26:10 -08:00
apiRouter.post("/play", async (req, res) => {
2025-02-15 00:37:32 -08:00
try {
await mediaPlayer.play();
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
2025-02-15 01:26:10 -08:00
apiRouter.post("/pause", async (req, res) => {
2025-02-15 00:37:32 -08:00
try {
await mediaPlayer.pause();
res.send(JSON.stringify({ success: true }));
} catch (error: any) {
res.status(500)
.send(JSON.stringify({ success: false, error: error.message }));
}
});
2025-02-15 01:26:10 -08:00
apiRouter.get("/nowplaying", async (req, res) => {
2025-02-15 00:37:32 -08:00
const nowPlaying = await mediaPlayer.getNowPlaying();
const pauseState = await mediaPlayer.getPauseState();
const volume = await mediaPlayer.getVolume();
const idle = await mediaPlayer.getIdle();
res.send(JSON.stringify({
success: true,
nowPlaying: nowPlaying,
isPaused: pauseState,
volume: volume,
isIdle: idle
}));
});
2025-02-15 01:26:10 -08:00
apiRouter.post("/volume", async (req, res) => {
2025-02-15 00:37:32 -08:00
const { volume } = req.body as { volume: number };
await mediaPlayer.setVolume(volume);
res.send(JSON.stringify({ success: true }));
});
2025-02-15 01:26:10 -08:00
apiRouter.ws("/events", (ws, req) => {
2025-02-15 00:37:32 -08:00
console.log(req.query);
mediaPlayer.subscribe(ws);
ws.on("close", () => {
mediaPlayer.unsubscribe(ws);
});
});
2025-02-15 01:26:10 -08:00
// Serve static files for React app (after building)
app.use(express.static("dist/frontend"));
// Mount API routes under /api
app.use("/api", apiRouter);
// Serve React app for all other routes (client-side routing)
app.get("*", (req, res) => {
res.sendFile("dist/frontend/index.html", { root: "." });
});
2025-02-15 00:37:32 -08:00
app.listen(3000, () => {
console.log("Server is running on port 3000");
});