From 8a011023023065518f37c043ab5863d0eb9e30f6 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sat, 23 Dec 2023 10:55:18 +0100 Subject: [PATCH] Move sse route to separate module --- src/main.rs | 20 +------------------- src/routes/mod.rs | 1 + src/routes/sse.rs | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 src/routes/sse.rs diff --git a/src/main.rs b/src/main.rs index 39e3f76..0595d9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,24 +2,6 @@ mod crate_version; mod mpd; mod routes; -async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result<()> { - // Update everything on connect - sender.send("playlist", "", None).await?; - sender.send("player", "", None).await?; - - let mut mpd = mpd::Mpd::new(); - mpd.connect().await.unwrap(); - - loop { - let systems = mpd - .idle(&["playlist", "player", "database", "options"]) - .await?; - for system in systems { - sender.send(&system, "", None).await?; - } - } -} - #[async_std::main] async fn main() -> tide::Result<()> { tracing_subscriber::fmt() @@ -34,7 +16,7 @@ async fn main() -> tide::Result<()> { app.at("/browser").get(routes::browser::get_browser); app.at("/art").get(routes::art::get_art); - app.at("/sse").get(tide::sse::endpoint(sse)); + app.at("/sse").get(tide::sse::endpoint(routes::sse::sse)); app.at("/queue").get(routes::queue::get_queue); app.at("/queue").post(routes::queue::post_queue); diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 4463182..6ef62c0 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -4,3 +4,4 @@ pub mod index; pub mod player; pub mod queue; pub mod controls; +pub mod sse; diff --git a/src/routes/sse.rs b/src/routes/sse.rs new file mode 100644 index 0000000..07f9752 --- /dev/null +++ b/src/routes/sse.rs @@ -0,0 +1,19 @@ +use crate::mpd; + +pub async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result<()> { + // Update everything on connect + sender.send("playlist", "", None).await?; + sender.send("player", "", None).await?; + + let mut mpd = mpd::Mpd::new(); + mpd.connect().await.unwrap(); + + loop { + let systems = mpd + .idle(&["playlist", "player", "database", "options"]) + .await?; + for system in systems { + sender.send(&system, "", None).await?; + } + } +} \ No newline at end of file