Add static serving from assets built into binary.
This commit is contained in:
30
src/web.rs
30
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<Extract = (impl warp::reply::Reply,), Error = Reject
|
||||
.with(warp::reply::with::headers(text_headers))
|
||||
}
|
||||
|
||||
fn index() -> Result<impl warp::Reply, warp::Rejection> {
|
||||
Ok("Hello world")
|
||||
fn index(path: warp::path::FullPath) -> Result<impl warp::Reply, warp::Rejection> {
|
||||
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<impl warp::Reply, warp::Rejection> {
|
||||
@@ -102,10 +121,15 @@ fn image(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "react-debug/build/"]
|
||||
struct Asset;
|
||||
|
||||
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::get2().and(warp::path::full()).and_then(index);
|
||||
|
||||
let albums = warp::path("albums").and(lib.clone()).and_then(albums);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user