Skip to content
1        10        20        30        40        50        60        70        80        901use actix_web::{2    HttpRequest, HttpResponse, Responder, get,3    http::header::{CACHE_CONTROL, HeaderValue},4    web,5};67use crate::config;89const HTMX: &str = include_str!("../assets/h.js");10const DAISYUI: &str = include_str!("../assets/daisyui.css");11const APP_CSS: &str = include_str!("../assets/app.css");1213#[get("/assets/{filename:.*}")]14pub async fn assets(req: HttpRequest, config: web::Data<config::Config>) -> impl Responder {15    let path = req.match_info().query("filename");16    let cache_control = config17        .cache_control18        .clone()19        .unwrap_or_else(|| HeaderValue::from_static(config::DEFAULT_CACHE_CONTROL));2021    match path {22        "h.js" => HttpResponse::Ok()23            .content_type("application/javascript; charset=utf-8")24            .insert_header((CACHE_CONTROL, cache_control))25            .body(HTMX),26        "daisyui.css" | "daisyui" => HttpResponse::Ok()27            .content_type("text/css; charset=utf-8")28            .insert_header((CACHE_CONTROL, cache_control))29            .body(DAISYUI),30        "app.css" => HttpResponse::Ok()31            .content_type("text/css; charset=utf-8")32            .insert_header((CACHE_CONTROL, cache_control))33            .body(APP_CSS),34        _ => HttpResponse::NotFound().body("Not found"),35    }36}3738#[cfg(test)]39mod tests {40    use super::*;41    use actix_web::{http::header::HeaderValue, test as aw_test, web};4243    #[actix_web::test]44    async fn test_assets_handler_known_files() {45        let config = config::Config::new(46            8080,47            HeaderValue::from_static(config::DEFAULT_CACHE_CONTROL),48            "pear.db".to_string(),49            false,50        );51        let app = aw_test::init_service(52            actix_web::App::new()53                .app_data(web::Data::new(config))54                .service(assets),55        )56        .await;5758        for (path, expected_type) in [59            ("/assets/h.js", "application/javascript"),60            ("/assets/daisyui.css", "text/css"),61            ("/assets/app.css", "text/css"),62        ] {63            let req = aw_test::TestRequest::get().uri(path).to_request();64            let resp = aw_test::call_service(&app, req).await;65            assert!(resp.status().is_success(), "{path} should succeed");66            assert!(67                resp.headers()68                    .get("content-type")69                    .unwrap()70                    .to_str()71                    .unwrap()72                    .starts_with(expected_type),73            );74        }75    }7677    #[actix_web::test]78    async fn test_assets_handler_unknown_file_returns_404() {79        let config = config::Config::new(80            8080,81            HeaderValue::from_static(config::DEFAULT_CACHE_CONTROL),82            "pear.db".to_string(),83            false,84        );85        let app = aw_test::init_service(86            actix_web::App::new()87                .app_data(web::Data::new(config))88                .service(assets),89        )90        .await;9192        let req = aw_test::TestRequest::get()93            .uri("/assets/unknown.txt")94            .to_request();95        let resp = aw_test::call_service(&app, req).await;96        assert_eq!(resp.status(), actix_web::http::StatusCode::NOT_FOUND);97    }98}