Move common code to lib module. More stub webservice.
This commit is contained in:
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
|
||||
|
||||
Reference in New Issue
Block a user