diff --git a/src/main.rs b/src/main.rs index 952b78b..30fa3d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ struct IndexQuery { path: String, } -async fn index(req: tide::Request<()>) -> tide::Result { +async fn get_index(req: tide::Request<()>) -> tide::Result { let query: IndexQuery = req.query()?; let entries = mpd::ls(&query.path)?; let queue = mpd::playlist()?; @@ -94,6 +94,28 @@ async fn get_player(_req: tide::Request<()>) -> tide::Result { Ok(template.into()) } +#[derive(Template)] +#[template(path = "browser.html")] +struct BrowserTemplate { + path: Vec, + entries: Vec, +} + +async fn get_browser(req: tide::Request<()>) -> tide::Result { + let query: IndexQuery = req.query()?; + let entries = mpd::ls(&query.path)?; + + let template = BrowserTemplate { + path: Path::new(&query.path) + .iter() + .map(|s| s.to_string_lossy().to_string()) + .collect(), + entries, + }; + + Ok(template.into()) +} + #[derive(Deserialize)] struct PostQueueQuery { path: String, @@ -139,7 +161,7 @@ async fn get_art(req: tide::Request<()>) -> tide::Result { async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result<()> { // Needs to be async and all async mpd libraries suck - let mut stream = TcpStream::connect(mpd::HOST).await?; + let mut stream = TcpStream::connect(mpd::host()).await?; let mut reader = BufReader::new(stream.clone()); // skip OK MPD line @@ -152,7 +174,7 @@ async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result sender.send("player", "", None).await?; loop { - stream.write_all(b"idle playlist player\n").await?; + stream.write_all(b"idle playlist player database\n").await?; loop { buffer.clear(); @@ -179,10 +201,11 @@ async fn main() -> tide::Result<()> { let mut app = tide::new(); app.with(tide_tracing::TraceMiddleware::new()); - app.at("/").get(index); + app.at("/").get(get_index); app.at("/queue").get(get_queue); app.at("/player").get(get_player); app.at("/art").get(get_art); + app.at("/browser").get(get_browser); app.at("/sse").get(tide::sse::endpoint(sse)); diff --git a/src/mpd.rs b/src/mpd.rs index b342630..61ade3b 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -2,10 +2,14 @@ use std::borrow::Cow; use mpdrs::lsinfo::LsInfoResponse; -pub(crate) const HOST: &str = "192.168.1.203:6600"; +pub(crate) fn host() -> String { + let host = std::env::var("MPD_HOST").unwrap_or("localhost".to_string()); + let port = std::env::var("MPD_PORT").unwrap_or("6600".to_string()); + format!("{host}:{port}") +} pub(crate) fn connect() -> Result { - mpdrs::Client::connect(HOST) + mpdrs::Client::connect(host()) } pub(crate) fn ls(path: &str) -> anyhow::Result> { @@ -41,7 +45,9 @@ pub(crate) fn ls(path: &str) -> anyhow::Result> { } pub(crate) struct QueueItem { + pub(crate) file: String, pub(crate) title: String, + pub(crate) artist: Option, pub(crate) playing: bool, } @@ -54,7 +60,9 @@ pub(crate) fn playlist() -> anyhow::Result> { .queue()? .into_iter() .map(|song| QueueItem { + file: song.file.clone(), title: song.title.as_ref().unwrap_or(&song.file).clone(), + artist: song.artist.clone(), playing: current == song.place, }) .collect(); diff --git a/templates/browser.html b/templates/browser.html new file mode 100644 index 0000000..11348b0 --- /dev/null +++ b/templates/browser.html @@ -0,0 +1,43 @@ +{# #} + + +
    + {% for entry in entries %} + {% match entry %} + {% when mpd::Entry::Song with { name, path, artist } %} +
  • + music_note + +
    +
    {{ name }}
    +
    {{ artist }}
    + +
  • + {% when mpd::Entry::Directory with { name, path }%} +
  • + folder + {{ name }} +
  • + {% when mpd::Entry::Playlist with { name, path } %} +
  • + playlist_play +
    +
    {{ name }}
    + +
  • + {% endmatch %} + {% endfor %} +
diff --git a/templates/index.html b/templates/index.html index 410a663..b4405f7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,12 +10,17 @@ - -
- - -
    - {% for entry in entries %} - {% match entry %} - {% when mpd::Entry::Song with { name, path, artist } %} -
  • - music_note - -
    -
    {{ name }}
    -
    {{ artist }}
    - -
  • - {% when mpd::Entry::Directory with { name, path }%} -
  • - folder - {{ name }} -
  • - {% when mpd::Entry::Playlist with { name, path } %} -
  • - playlist_play -
    -
    {{ name }}
    - -
  • - {% endmatch %} - {% endfor %} -
+ +
+ {% include "browser.html" %}
-
-
-
+
+
+ {% include "player.html" %} +
+
+ {% include "queue.html" %} +
diff --git a/templates/player.html b/templates/player.html index 0860406..522e22a 100644 --- a/templates/player.html +++ b/templates/player.html @@ -3,12 +3,10 @@
{% if let Some(song) = song %} - - +
+ +
+