diff --git a/Cargo.lock b/Cargo.lock index b3c201d..37f64e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1314,9 +1314,11 @@ dependencies = [ "image", "lazy_static 1.4.0", "log 0.4.8", + "mime_guess 2.0.1", "prometheus", "regex", "reqwest", + "rust-embed", "serde", "serde_json", "stderrlog", @@ -1759,6 +1761,38 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "rust-embed" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "412cddb9905679491f2829ef7fea79179b9a76fa942e06ab52b420dbab94a406" +dependencies = [ + "rust-embed-impl", + "rust-embed-utils", + "walkdir", +] + +[[package]] +name = "rust-embed-impl" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50212d0e652f580e6d297537c31237d4b2f4497e5912eebe25fde97ac06a51df" +dependencies = [ + "quote", + "rust-embed-utils", + "syn", + "walkdir", +] + +[[package]] +name = "rust-embed-utils" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97655158074ccb2d2cfb1ccb4c956ef0f4054e43a2c1e71146d4991e6961e105" +dependencies = [ + "walkdir", +] + [[package]] name = "rustc_version" version = "0.1.7" @@ -1825,6 +1859,15 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.17" @@ -2634,6 +2677,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +[[package]] +name = "walkdir" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +dependencies = [ + "same-file", + "winapi 0.3.8", + "winapi-util", +] + [[package]] name = "want" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index a4f011d..a9984f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ yup-oauth2 = "^3.1" warp = "0.1" serde = { version = "1.0.104", features = ["derive"] } image = "0.23.0" +rust-embed = "5.2.0" +mime_guess = "2.0.1" [dependencies.prometheus] features = ["process"] diff --git a/src/web.rs b/src/web.rs index 22e0932..b4e710d 100644 --- a/src/web.rs +++ b/src/web.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use log::info; use log::warn; use prometheus::Encoder; +use rust_embed::RustEmbed; use serde::Deserialize; use warp; use warp::http::header::{HeaderMap, HeaderValue}; @@ -31,8 +32,26 @@ fn metrics() -> impl Filter Result { - Ok("Hello world") +fn index(path: warp::path::FullPath) -> Result { + let path = path.as_str(); + let path = if path.ends_with("/") { + format!("{}index.html", path.to_string()) + } else { + path.to_string() + }; + let path = &path[1..]; + + match Asset::get(path) { + Some(bytes) => { + let mime = mime_guess::from_path(path).first_or_octet_stream(); + + Ok(warp::http::Response::builder() + .header("Content-Type", mime.essence_str()) + .header("Content-Length", bytes.len()) + .body(bytes.into_owned())) + } + None => Err(warp::reject::not_found()), + } } fn albums(lib: Library) -> Result { @@ -102,10 +121,15 @@ fn image( } } +#[derive(RustEmbed)] +#[folder = "react-debug/build/"] +struct Asset; + 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 index = warp::get2().and(warp::path::full()).and_then(index); let albums = warp::path("albums").and(lib.clone()).and_then(albums);