Coverage Report

Created: 2024-02-20 21:15

/builds/xfbs/cindy/src/server/frontend.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::Cindy;
2
use axum::{
3
    http::{header::CONTENT_TYPE, HeaderName, HeaderValue, Uri},
4
    Router,
5
};
6
use include_dir::{include_dir, Dir};
7
8
/// Frontend files baked-in to the Cindy binary for convenience.
9
static FRONTEND_FILES: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/ui/dist");
10
11
/// By default, the index HTML file is served.
12
const DEFAULT_FILE: &str = "index.html";
13
14
/// Serve frontend static file.
15
2
async fn static_file(uri: Uri) -> ([(HeaderName, HeaderValue); 1], &'static [u8]) {
16
2
    let file = match FRONTEND_FILES.get_file(uri.path().trim_start_matches('/')) {
17
1
        Some(file) => file,
18
1
        None => FRONTEND_FILES.get_file(DEFAULT_FILE).unwrap(),
19
    };
20
2
    let guess = mime_guess::from_path(file.path());
21
2
    let mime = guess
22
2
        .first_raw()
23
2
        .map(HeaderValue::from_static)
24
2
        .unwrap_or_else(|| 
HeaderValue::from_str(mime::APPLICATION_OCTET_STREAM.as_ref()).unwrap()0
);
25
2
    ([(CONTENT_TYPE, mime)], file.contents())
26
2
}
27
28
16
pub fn router() -> Router<Cindy> {
29
16
    Router::new().fallback(static_file)
30
16
}