Files
QueueCube/backend/src/server.ts

211 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-02-15 00:37:32 -08:00
import express from "express";
import expressWs from "express-ws";
2025-03-02 18:54:17 -08:00
import path from "path";
2025-02-15 00:37:32 -08:00
import { MediaPlayer } from "./MediaPlayer";
2025-02-23 13:46:31 -08:00
import { searchInvidious, fetchThumbnail } from "./InvidiousAPI";
import { PlaylistItem } from './types';
2025-02-15 00:37:32 -08:00
const app = express();
app.use(express.json());
2025-02-15 02:37:17 -08:00
expressWs(app);
2025-02-15 00:37:32 -08:00
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 02:37:17 -08:00
const withErrorHandling = (func: (req: any, res: any) => Promise<any>) => {
return async (req: any, res: any) => {
try {
await func(req, res);
} catch (error: any) {
res.status(500).send(JSON.stringify({ success: false, error: error.message }));
}
};
};
apiRouter.get("/playlist", withErrorHandling(async (req, res) => {
2025-02-15 00:37:32 -08:00
const playlist = await mediaPlayer.getPlaylist();
res.send(playlist);
2025-02-15 02:37:17 -08:00
}));
2025-02-15 00:37:32 -08:00
2025-02-15 02:37:17 -08:00
apiRouter.post("/playlist", withErrorHandling(async (req, res) => {
const { url } = req.body as { url: string };
await mediaPlayer.append(url);
res.send(JSON.stringify({ success: true }));
}));
2025-02-15 00:37:32 -08:00
2025-02-15 02:37:17 -08:00
apiRouter.delete("/playlist/:index", withErrorHandling(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 02:37:17 -08:00
}));
2025-02-15 00:37:32 -08:00
2025-02-23 13:46:31 -08:00
apiRouter.post("/playlist/replace", withErrorHandling(async (req, res) => {
const { url } = req.body as { url: string };
await mediaPlayer.replace(url);
res.send(JSON.stringify({ success: true }));
}));
2025-02-15 02:37:17 -08:00
apiRouter.post("/play", withErrorHandling(async (req, res) => {
await mediaPlayer.play();
res.send(JSON.stringify({ success: true }));
}));
2025-02-15 00:37:32 -08:00
2025-02-15 02:37:17 -08:00
apiRouter.post("/pause", withErrorHandling(async (req, res) => {
await mediaPlayer.pause();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.post("/skip", withErrorHandling(async (req, res) => {
await mediaPlayer.skip();
res.send(JSON.stringify({ success: true }));
}));
2025-02-15 00:37:32 -08:00
2025-02-15 16:28:47 -08:00
apiRouter.post("/skip/:index", withErrorHandling(async (req, res) => {
const { index } = req.params as { index: string };
await mediaPlayer.skipTo(parseInt(index));
res.send(JSON.stringify({ success: true }));
}));
2025-02-15 02:37:17 -08:00
apiRouter.post("/previous", withErrorHandling(async (req, res) => {
await mediaPlayer.previous();
res.send(JSON.stringify({ success: true }));
}));
apiRouter.get("/nowplaying", withErrorHandling(async (req, res) => {
2025-02-15 22:15:59 -08:00
const playingItem = await mediaPlayer.getNowPlaying();
2025-02-15 02:37:17 -08:00
const currentFile = await mediaPlayer.getCurrentFile();
2025-02-15 00:37:32 -08:00
const pauseState = await mediaPlayer.getPauseState();
const volume = await mediaPlayer.getVolume();
const idle = await mediaPlayer.getIdle();
res.send(JSON.stringify({
success: true,
2025-02-15 22:15:59 -08:00
playingItem: playingItem,
2025-02-15 00:37:32 -08:00
isPaused: pauseState,
volume: volume,
2025-02-15 02:37:17 -08:00
isIdle: idle,
currentFile: currentFile
2025-02-15 00:37:32 -08:00
}));
2025-02-15 02:37:17 -08:00
}));
2025-02-15 00:37:32 -08:00
2025-02-15 02:37:17 -08:00
apiRouter.post("/volume", withErrorHandling(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 02:37:17 -08:00
}));
2025-02-15 00:37:32 -08:00
2025-02-15 01:26:10 -08:00
apiRouter.ws("/events", (ws, req) => {
2025-02-15 02:19:14 -08:00
console.log("Events client connected");
2025-02-15 00:37:32 -08:00
mediaPlayer.subscribe(ws);
ws.on("close", () => {
2025-02-15 02:19:14 -08:00
console.log("Events client disconnected");
2025-02-15 00:37:32 -08:00
mediaPlayer.unsubscribe(ws);
});
});
2025-02-22 01:09:33 -08:00
apiRouter.get("/search", withErrorHandling(async (req, res) => {
const query = req.query.q as string;
if (!query) {
res.status(400)
.send(JSON.stringify({ success: false, error: "Query parameter 'q' is required" }));
return;
}
const results = await searchInvidious(query);
res.send(JSON.stringify({ success: true, results }));
}));
apiRouter.get("/thumbnail", withErrorHandling(async (req, res) => {
const thumbnailUrl = req.query.url as string;
if (!thumbnailUrl) {
res.status(400)
.send(JSON.stringify({ success: false, error: "URL parameter is required" }));
return;
}
try {
2025-02-22 02:18:17 -08:00
const { data, contentType } = await fetchThumbnail(thumbnailUrl);
res.set('Content-Type', contentType);
data.pipe(res);
2025-02-22 01:09:33 -08:00
} catch (error) {
console.error('Failed to proxy thumbnail:', error);
res.status(500)
.send(JSON.stringify({ success: false, error: 'Failed to fetch thumbnail' }));
}
}));
2025-02-23 13:46:31 -08:00
apiRouter.get("/favorites", withErrorHandling(async (req, res) => {
const favorites = await mediaPlayer.getFavorites();
res.send(JSON.stringify(favorites));
}));
apiRouter.post("/favorites", withErrorHandling(async (req, res) => {
const { filename } = req.body as { filename: string };
console.log("Adding favorite: " + filename);
await mediaPlayer.addFavorite(filename);
2025-02-23 13:46:31 -08:00
res.send(JSON.stringify({ success: true }));
}));
apiRouter.delete("/favorites/:filename", withErrorHandling(async (req, res) => {
const { filename } = req.params as { filename: string };
await mediaPlayer.removeFavorite(filename);
2025-02-23 13:46:31 -08:00
res.send(JSON.stringify({ success: true }));
}));
apiRouter.delete("/favorites", withErrorHandling(async (req, res) => {
await mediaPlayer.clearFavorites();
res.send(JSON.stringify({ success: true }));
2025-03-05 00:10:23 -08:00
}));
apiRouter.put("/favorites/:filename/title", withErrorHandling(async (req, res) => {
const { filename } = req.params as { filename: string };
const { title } = req.body as { title: string };
if (!title) {
res.status(400).send(JSON.stringify({
success: false,
error: "Title is required"
}));
return;
}
await mediaPlayer.updateFavoriteTitle(filename, title);
res.send(JSON.stringify({ success: true }));
2025-02-23 13:46:31 -08:00
}));
2025-02-15 01:26:10 -08:00
// Serve static files for React app (after building)
2025-03-02 18:54:17 -08:00
app.use(express.static(path.join(__dirname, "../dist/frontend")));
2025-02-15 01:26:10 -08:00
// Mount API routes under /api
app.use("/api", apiRouter);
// Serve React app for all other routes (client-side routing)
app.get("*", (req, res) => {
2025-03-02 18:54:17 -08:00
res.sendFile(path.join(__dirname, "../dist/frontend/index.html"));
2025-02-15 01:26:10 -08:00
});
2025-02-15 00:37:32 -08:00
2025-02-15 16:45:59 -08:00
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// Add graceful shutdown handling
const shutdown = async () => {
console.log('Received shutdown signal. Closing server...');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
// Force termination after some timeout (10sec)
setTimeout(() => {
console.log('Forcing server shutdown');
process.exit(1);
}, 10000);
};
// Handle various shutdown signals
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);