initial commit
This commit is contained in:
93
src/server.ts
Normal file
93
src/server.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
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());
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const mediaPlayer = new MediaPlayer();
|
||||
|
||||
router.get("/playlist", async (req, res) => {
|
||||
const playlist = await mediaPlayer.getPlaylist();
|
||||
res.send(playlist);
|
||||
});
|
||||
|
||||
router.post("/playlist", async (req, res) => {
|
||||
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 }));
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/playlist/:index", async (req, res) => {
|
||||
const { index } = req.params as { index: string };
|
||||
await mediaPlayer.deletePlaylistItem(parseInt(index));
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
});
|
||||
|
||||
router.post("/play", async (req, res) => {
|
||||
try {
|
||||
await mediaPlayer.play();
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
} catch (error: any) {
|
||||
res.status(500)
|
||||
.send(JSON.stringify({ success: false, error: error.message }));
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/pause", async (req, res) => {
|
||||
try {
|
||||
await mediaPlayer.pause();
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
} catch (error: any) {
|
||||
res.status(500)
|
||||
.send(JSON.stringify({ success: false, error: error.message }));
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/nowplaying", async (req, res) => {
|
||||
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
|
||||
}));
|
||||
});
|
||||
|
||||
router.post("/volume", async (req, res) => {
|
||||
const { volume } = req.body as { volume: number };
|
||||
await mediaPlayer.setVolume(volume);
|
||||
res.send(JSON.stringify({ success: true }));
|
||||
});
|
||||
|
||||
router.ws("/events", (ws, req) => {
|
||||
console.log(req.query);
|
||||
mediaPlayer.subscribe(ws);
|
||||
|
||||
ws.on("close", () => {
|
||||
mediaPlayer.unsubscribe(ws);
|
||||
});
|
||||
});
|
||||
|
||||
app.use("/", router);
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log("Server is running on port 3000");
|
||||
});
|
||||
Reference in New Issue
Block a user