From e191b2a3ffc9a41999fdccf221d4493137c474ea Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 10 Feb 2020 21:30:20 -0800 Subject: [PATCH] First version of rust web server serving album.json --- react-debug/src/App.js | 10 +++++----- src/library.rs | 5 +++++ src/web.rs | 8 +++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/react-debug/src/App.js b/react-debug/src/App.js index afa4815..18b7c36 100644 --- a/react-debug/src/App.js +++ b/react-debug/src/App.js @@ -12,7 +12,7 @@ class Album extends React.Component { } componentDidMount() { let {album} = this.props; - fetch(process.env.PUBLIC_URL + `/photosync/${album}/album.json`) + fetch(process.env.PUBLIC_URL + `/api/album/${album}`) .then(res => res.json()) .then( (result) => this.setState({media_items: result}), @@ -30,7 +30,7 @@ class Album extends React.Component { // TODO(wathiede): use coverPhotoMediaItemId and fetch from a // locally cached image. return
- { + {

{ mi.filename}

@@ -53,7 +53,7 @@ class AlbumIndex extends React.Component { }; } componentDidMount() { - fetch(process.env.PUBLIC_URL + "/photosync/albums.json") + fetch(process.env.PUBLIC_URL + "/api/albums") .then(res => res.json()) .then( (result) => this.setState({albums: result}), @@ -68,8 +68,8 @@ class AlbumIndex extends React.Component { console.log(albums); return albums.map((a) => { let img = unset; - if (a.coverPhotoBaseUrl !== undefined) { - img = {; + if (a.coverPhotoMediaItemId !== undefined) { + img = { } let figure =
diff --git a/src/library.rs b/src/library.rs index 684f447..ca48cfb 100644 --- a/src/library.rs +++ b/src/library.rs @@ -53,6 +53,11 @@ impl Library { info!("saving {}", path.to_string_lossy()); fs::write(path, j) } + pub fn album(&self, album_id: &str) -> Result, Box> { + 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, diff --git a/src/web.rs b/src/web.rs index a28a759..8846fe0 100644 --- a/src/web.rs +++ b/src/web.rs @@ -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 { } fn album(lib: Library, id: String) -> Result { - 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> { 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())