From 1c993967058f59f03bd8abc0dc4fa2865153bf21 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 10 Feb 2020 21:13:46 -0800 Subject: [PATCH] Move common code to lib module. More stub webservice. --- src/lib.rs | 2 ++ src/library.rs | 1 + src/main.rs | 10 +++------- src/web.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..07f3391 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod library; +pub mod web; diff --git a/src/library.rs b/src/library.rs index 151c4a3..684f447 100644 --- a/src/library.rs +++ b/src/library.rs @@ -9,6 +9,7 @@ use log::info; use photos::schemas::Album; use photos::schemas::MediaItem; +#[derive(Clone)] pub struct Library { root: PathBuf, originals_dir: PathBuf, diff --git a/src/main.rs b/src/main.rs index ec61d0c..c8ae486 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,5 @@ use std::collections::HashMap; use std::error::Error; -use std::fs; -use std::fs::File; -use std::io; use std::net::SocketAddr; use std::path::PathBuf; @@ -13,12 +10,11 @@ use lazy_static::lazy_static; use log::{debug, info}; use photos::schemas::{Album, MediaItem, SearchMediaItemsRequest}; use regex::Regex; -use reqwest; use structopt::StructOpt; use yup_oauth2::{Authenticator, InstalledFlow}; -mod library; -mod web; +use photosync::library::Library; +use photosync::web; #[derive(Debug, StructOpt)] enum Command { @@ -199,7 +195,7 @@ fn sync_albums( title_filter: Option, output_dir: PathBuf, ) -> Result<(), Box> { - let lib = library::Library::new(output_dir)?; + let lib = Library::new(output_dir)?; let albums = list_albums(client, title_filter)?; lib.create_album_index(&albums)?; for a in &albums { diff --git a/src/web.rs b/src/web.rs index 22a0fde..a28a759 100644 --- a/src/web.rs +++ b/src/web.rs @@ -2,13 +2,15 @@ use std::error::Error; use std::net::SocketAddr; use std::path::PathBuf; -use log::info; use prometheus::Encoder; +use serde::Deserialize; use warp; use warp::http::header::{HeaderMap, HeaderValue}; use warp::reject::Rejection; use warp::Filter; +use crate::library::Library; + fn metrics() -> impl Filter + Clone { let mut text_headers = HeaderMap::new(); text_headers.insert("content-type", HeaderValue::from_static("text/plain")); @@ -31,12 +33,45 @@ fn index() -> Result { Ok("Hello world") } +fn album(lib: Library, id: String) -> Result { + Ok(format!("Hello world: {}", id)) +} + +#[derive(Debug, Deserialize)] +struct ImageParams { + w: Option, + h: Option, + c: Option, +} + +fn image( + lib: Library, + image_id: String, + params: ImageParams, +) -> Result { + Ok(format!("Hello world: {} {:?}", image_id, params)) +} + pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box> { + let lib = Library::new(root)?; + let lib = warp::any().map(move || lib.clone()); let index = warp::path::end().and_then(index); + let album = warp::path("album") + .and(lib.clone()) + .and(warp::path::param()) + .and_then(album); + let image = warp::path("image") + .and(lib.clone()) + .and(warp::path::param()) + .and(warp::query::()) + .and_then(image); + + let api = album.or(image); + let api = warp::path("api").and(api); + // Fallback, always keep this last. - //let api = api.or(index); - let api = index; + let api = api.or(index); let api = api.with(warp::log("photosync")); // We don't want metrics & heath checking filling up the logs, so we add this handler after