Simplify mpd calls
This commit is contained in:
parent
95270543e4
commit
96a514d3c6
38
src/main.rs
38
src/main.rs
|
@ -26,7 +26,7 @@ struct IndexQuery {
|
||||||
|
|
||||||
async fn index(req: tide::Request<()>) -> tide::Result {
|
async fn index(req: tide::Request<()>) -> tide::Result {
|
||||||
let query: IndexQuery = req.query()?;
|
let query: IndexQuery = req.query()?;
|
||||||
let entries = mpd::ls(&query.path).await?;
|
let entries = mpd::ls(&query.path)?;
|
||||||
let template = IndexTemplate {
|
let template = IndexTemplate {
|
||||||
path: Path::new(&query.path)
|
path: Path::new(&query.path)
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -44,7 +44,7 @@ struct QueueTemplate {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_queue(_req: tide::Request<()>) -> tide::Result {
|
async fn get_queue(_req: tide::Request<()>) -> tide::Result {
|
||||||
let queue = mpd::playlist().await?;
|
let queue = mpd::playlist()?;
|
||||||
let template = QueueTemplate { queue };
|
let template = QueueTemplate { queue };
|
||||||
Ok(template.into())
|
Ok(template.into())
|
||||||
}
|
}
|
||||||
|
@ -55,30 +55,28 @@ struct PostQueueQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_queue(req: tide::Request<()>) -> tide::Result {
|
async fn post_queue(req: tide::Request<()>) -> tide::Result {
|
||||||
let mut client = mpdrs::Client::connect(mpd::HOST)?;
|
|
||||||
let query: PostQueueQuery = req.query()?;
|
let query: PostQueueQuery = req.query()?;
|
||||||
client.add(&query.path)?;
|
mpd::connect()?.add(&query.path)?;
|
||||||
Ok("".into())
|
Ok("".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_play(_req: tide::Request<()>) -> tide::Result {
|
async fn post_play(_req: tide::Request<()>) -> tide::Result {
|
||||||
let mut mpd = mpdrs::Client::connect(mpd::HOST)?;
|
mpd::connect()?.play()?;
|
||||||
mpd.play()?;
|
|
||||||
Ok("".into())
|
Ok("".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_pause(_req: tide::Request<()>) -> tide::Result {
|
async fn post_pause(_req: tide::Request<()>) -> tide::Result {
|
||||||
let mut mpd = mpdrs::Client::connect(mpd::HOST)?;
|
mpd::connect()?.pause(true)?;
|
||||||
mpd.pause(true)?;
|
|
||||||
Ok("".into())
|
Ok("".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_previous(_req: tide::Request<()>) -> tide::Result {
|
async fn post_previous(_req: tide::Request<()>) -> tide::Result {
|
||||||
let mut mpd = mpdrs::Client::connect(mpd::HOST)?;
|
mpd::connect()?.prev()?;
|
||||||
mpd.prev()?;
|
|
||||||
Ok("".into())
|
Ok("".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_next(_req: tide::Request<()>) -> tide::Result {
|
async fn post_next(_req: tide::Request<()>) -> tide::Result {
|
||||||
let mut mpd = mpdrs::Client::connect(mpd::HOST)?;
|
mpd::connect()?.next()?;
|
||||||
mpd.next()?;
|
|
||||||
Ok("".into())
|
Ok("".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,11 +95,11 @@ async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result
|
||||||
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
reader.read_line(&mut buffer).await?;
|
reader.read_line(&mut buffer).await?;
|
||||||
if buffer == "changed: playlist\n" {
|
let (_, changed) = buffer
|
||||||
sender.send("queue", "", None).await?;
|
.trim_end()
|
||||||
} else if buffer == "changed: player\n" {
|
.split_once(": ")
|
||||||
sender.send("player", "", None).await?;
|
.ok_or(anyhow!("unexpected response from MPD"))?;
|
||||||
}
|
sender.send(changed, "", None).await?;
|
||||||
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
reader.read_line(&mut buffer).await?;
|
reader.read_line(&mut buffer).await?;
|
||||||
|
@ -119,14 +117,18 @@ async fn main() -> tide::Result<()> {
|
||||||
|
|
||||||
let mut app = tide::new();
|
let mut app = tide::new();
|
||||||
app.with(tide_tracing::TraceMiddleware::new());
|
app.with(tide_tracing::TraceMiddleware::new());
|
||||||
|
|
||||||
app.at("/").get(index);
|
app.at("/").get(index);
|
||||||
app.at("/queue").get(get_queue);
|
app.at("/queue").get(get_queue);
|
||||||
|
|
||||||
|
app.at("/sse").get(tide::sse::endpoint(sse));
|
||||||
|
|
||||||
app.at("/queue").post(post_queue);
|
app.at("/queue").post(post_queue);
|
||||||
app.at("/play").post(post_play);
|
app.at("/play").post(post_play);
|
||||||
app.at("/pause").post(post_pause);
|
app.at("/pause").post(post_pause);
|
||||||
app.at("/previous").post(post_previous);
|
app.at("/previous").post(post_previous);
|
||||||
app.at("/next").post(post_next);
|
app.at("/next").post(post_next);
|
||||||
app.at("/sse").get(tide::sse::endpoint(sse));
|
|
||||||
app.listen("0.0.0.0:8080").await?;
|
app.listen("0.0.0.0:8080").await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
20
src/mpd.rs
20
src/mpd.rs
|
@ -4,9 +4,12 @@ use mpdrs::lsinfo::LsInfoResponse;
|
||||||
|
|
||||||
pub(crate) const HOST: &str = "192.168.1.203:6600";
|
pub(crate) const HOST: &str = "192.168.1.203:6600";
|
||||||
|
|
||||||
pub(crate) async fn ls(path: &str) -> anyhow::Result<Vec<Entry>> {
|
pub(crate) fn connect() -> Result<mpdrs::Client, mpdrs::error::Error> {
|
||||||
let mut mpd = mpdrs::Client::connect(HOST)?;
|
mpdrs::Client::connect(HOST)
|
||||||
let info = mpd.lsinfo(path)?;
|
}
|
||||||
|
|
||||||
|
pub(crate) fn ls(path: &str) -> anyhow::Result<Vec<Entry>> {
|
||||||
|
let info = connect()?.lsinfo(path)?;
|
||||||
|
|
||||||
fn filename(path: &str) -> Cow<str> {
|
fn filename(path: &str) -> Cow<str> {
|
||||||
std::path::Path::new(path)
|
std::path::Path::new(path)
|
||||||
|
@ -24,12 +27,10 @@ pub(crate) async fn ls(path: &str) -> anyhow::Result<Vec<Entry>> {
|
||||||
path: song.file.clone(),
|
path: song.file.clone(),
|
||||||
},
|
},
|
||||||
|
|
||||||
LsInfoResponse::Directory { path, .. } => {
|
LsInfoResponse::Directory { path, .. } => Entry::Directory {
|
||||||
Entry::Directory {
|
|
||||||
name: filename(path).to_string(),
|
name: filename(path).to_string(),
|
||||||
path: path.to_string(),
|
path: path.to_string(),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
LsInfoResponse::Playlist { path, .. } => Entry::Playlist {
|
LsInfoResponse::Playlist { path, .. } => Entry::Playlist {
|
||||||
name: filename(path).to_string(),
|
name: filename(path).to_string(),
|
||||||
|
@ -44,8 +45,8 @@ pub(crate) struct QueueItem {
|
||||||
pub(crate) playing: bool,
|
pub(crate) playing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn playlist() -> anyhow::Result<Vec<QueueItem>> {
|
pub(crate) fn playlist() -> anyhow::Result<Vec<QueueItem>> {
|
||||||
let mut client = mpdrs::Client::connect(HOST)?;
|
let mut client = connect()?;
|
||||||
|
|
||||||
let current = client.status()?.song;
|
let current = client.status()?.song;
|
||||||
|
|
||||||
|
@ -57,6 +58,7 @@ pub(crate) async fn playlist() -> anyhow::Result<Vec<QueueItem>> {
|
||||||
playing: current == song.place,
|
playing: current == song.place,
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(queue)
|
Ok(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue