assets.rs

 1use anyhow::anyhow;
 2use rust_embed::RustEmbed;
 3use tide::{http::mime, Server};
 4
 5#[derive(RustEmbed)]
 6#[folder = "static"]
 7struct Static;
 8
 9pub fn add_routes(app: &mut Server<()>) {
10    app.at("/*path").get(get_static_asset);
11}
12
13async fn get_static_asset(request: tide::Request<()>) -> tide::Result {
14    let path = request.param("path").unwrap();
15    let content = Static::get(path).ok_or_else(|| anyhow!("asset not found at {}", path))?;
16
17    let content_type = if path.starts_with("svg") {
18        mime::SVG
19    } else if path.starts_with("styles") {
20        mime::CSS
21    } else {
22        mime::BYTE_STREAM
23    };
24
25    Ok(tide::Response::builder(200)
26        .content_type(content_type)
27        .body(content.data.as_ref())
28        .build())
29}