First version of rust web server serving album.json

This commit is contained in:
2020-02-10 21:30:20 -08:00
parent 1c99396705
commit e191b2a3ff
3 changed files with 17 additions and 6 deletions

View File

@@ -53,6 +53,11 @@ impl Library {
info!("saving {}", path.to_string_lossy());
fs::write(path, j)
}
pub fn album(&self, album_id: &str) -> Result<Vec<MediaItem>, Box<dyn std::error::Error>> {
let album_path = self.root.join(album_id).join("album.json");
let bytes = fs::read(album_path)?;
Ok(serde_json::from_slice(&bytes)?)
}
pub fn download_image(
&self,
filename: &str,

View File

@@ -2,6 +2,7 @@ use std::error::Error;
use std::net::SocketAddr;
use std::path::PathBuf;
use log::warn;
use prometheus::Encoder;
use serde::Deserialize;
use warp;
@@ -34,7 +35,11 @@ fn index() -> Result<impl warp::Reply, warp::Rejection> {
}
fn album(lib: Library, id: String) -> Result<impl warp::Reply, warp::Rejection> {
Ok(format!("Hello world: {}", id))
let album = lib.album(&id).map_err(|e| {
warn!("Couldn't find album {}: {}", id, e);
warp::reject::not_found()
})?;
Ok(warp::reply::json(&album))
}
#[derive(Debug, Deserialize)]
@@ -57,6 +62,7 @@ pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
let lib = warp::any().map(move || lib.clone());
let index = warp::path::end().and_then(index);
// TODO(wathiede): implement album index
let album = warp::path("album")
.and(lib.clone())
.and(warp::path::param())