Move common code to lib module. More stub webservice.

This commit is contained in:
Bill Thiede 2020-02-10 21:13:46 -08:00
parent 5c0f409f03
commit 1c99396705
4 changed files with 44 additions and 10 deletions

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod library;
pub mod web;

View File

@ -9,6 +9,7 @@ use log::info;
use photos::schemas::Album; use photos::schemas::Album;
use photos::schemas::MediaItem; use photos::schemas::MediaItem;
#[derive(Clone)]
pub struct Library { pub struct Library {
root: PathBuf, root: PathBuf,
originals_dir: PathBuf, originals_dir: PathBuf,

View File

@ -1,8 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fs;
use std::fs::File;
use std::io;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
@ -13,12 +10,11 @@ use lazy_static::lazy_static;
use log::{debug, info}; use log::{debug, info};
use photos::schemas::{Album, MediaItem, SearchMediaItemsRequest}; use photos::schemas::{Album, MediaItem, SearchMediaItemsRequest};
use regex::Regex; use regex::Regex;
use reqwest;
use structopt::StructOpt; use structopt::StructOpt;
use yup_oauth2::{Authenticator, InstalledFlow}; use yup_oauth2::{Authenticator, InstalledFlow};
mod library; use photosync::library::Library;
mod web; use photosync::web;
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
enum Command { enum Command {
@ -199,7 +195,7 @@ fn sync_albums(
title_filter: Option<Regex>, title_filter: Option<Regex>,
output_dir: PathBuf, output_dir: PathBuf,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let lib = library::Library::new(output_dir)?; let lib = Library::new(output_dir)?;
let albums = list_albums(client, title_filter)?; let albums = list_albums(client, title_filter)?;
lib.create_album_index(&albums)?; lib.create_album_index(&albums)?;
for a in &albums { for a in &albums {

View File

@ -2,13 +2,15 @@ use std::error::Error;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use log::info;
use prometheus::Encoder; use prometheus::Encoder;
use serde::Deserialize;
use warp; use warp;
use warp::http::header::{HeaderMap, HeaderValue}; use warp::http::header::{HeaderMap, HeaderValue};
use warp::reject::Rejection; use warp::reject::Rejection;
use warp::Filter; use warp::Filter;
use crate::library::Library;
fn metrics() -> impl Filter<Extract = (impl warp::reply::Reply,), Error = Rejection> + Clone { fn metrics() -> impl Filter<Extract = (impl warp::reply::Reply,), Error = Rejection> + Clone {
let mut text_headers = HeaderMap::new(); let mut text_headers = HeaderMap::new();
text_headers.insert("content-type", HeaderValue::from_static("text/plain")); text_headers.insert("content-type", HeaderValue::from_static("text/plain"));
@ -31,12 +33,45 @@ fn index() -> Result<impl warp::Reply, warp::Rejection> {
Ok("Hello world") Ok("Hello world")
} }
fn album(lib: Library, id: String) -> Result<impl warp::Reply, warp::Rejection> {
Ok(format!("Hello world: {}", id))
}
#[derive(Debug, Deserialize)]
struct ImageParams {
w: Option<usize>,
h: Option<usize>,
c: Option<bool>,
}
fn image(
lib: Library,
image_id: String,
params: ImageParams,
) -> Result<impl warp::Reply, warp::Rejection> {
Ok(format!("Hello world: {} {:?}", image_id, params))
}
pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> { pub fn run(addr: SocketAddr, root: PathBuf) -> Result<(), Box<dyn Error>> {
let lib = Library::new(root)?;
let lib = warp::any().map(move || lib.clone());
let index = warp::path::end().and_then(index); 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::<ImageParams>())
.and_then(image);
let api = album.or(image);
let api = warp::path("api").and(api);
// Fallback, always keep this last. // Fallback, always keep this last.
//let api = api.or(index); let api = api.or(index);
let api = index;
let api = api.with(warp::log("photosync")); let api = api.with(warp::log("photosync"));
// We don't want metrics & heath checking filling up the logs, so we add this handler after // We don't want metrics & heath checking filling up the logs, so we add this handler after