Move common code to lib module. More stub webservice.
This commit is contained in:
parent
5c0f409f03
commit
1c99396705
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod library;
|
||||
pub mod web;
|
||||
@ -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,
|
||||
|
||||
10
src/main.rs
10
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<Regex>,
|
||||
output_dir: PathBuf,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
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 {
|
||||
|
||||
41
src/web.rs
41
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<Extract = (impl warp::reply::Reply,), Error = Rejection> + Clone {
|
||||
let mut text_headers = HeaderMap::new();
|
||||
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")
|
||||
}
|
||||
|
||||
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>> {
|
||||
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::<ImageParams>())
|
||||
.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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user