|
| 1 | +use super::error::Nope; |
| 2 | +use iron::{ |
| 3 | + headers::CacheDirective, |
| 4 | + headers::{CacheControl, ContentType}, |
| 5 | + status::Status, |
| 6 | + IronError, IronResult, Request, Response, |
| 7 | +}; |
| 8 | +use router::Router; |
| 9 | + |
| 10 | +const EOT: &str = "application/vnd.ms-fontobject"; |
| 11 | +const TTF: &str = "application/x-font-ttf"; |
| 12 | +const WOFF2: &str = "application/font-woff2"; |
| 13 | +const SVG: &str = "image/svg+xml"; |
| 14 | +const WOFF: &str = "application/font-woff"; |
| 15 | + |
| 16 | +macro_rules! serve_static_files { |
| 17 | + ($($file_name:literal => $content_type:expr),* $(,)?) => { |
| 18 | + // Generate the static file handler |
| 19 | + pub fn static_handler(req: &mut Request) -> IronResult<Response> { |
| 20 | + let router = extension!(req, Router); |
| 21 | + let file = cexpect!(req, router.find("file")); |
| 22 | + |
| 23 | + // Select which file the user requested or return an error if it doesn't exist |
| 24 | + let (contents, content_type): (&'static [u8], ContentType) = match file { |
| 25 | + $( |
| 26 | + $file_name => ( |
| 27 | + include_bytes!(concat!("../../vendor/fontawesome/webfonts/", $file_name)), |
| 28 | + ContentType($content_type.parse().unwrap()), |
| 29 | + ), |
| 30 | + )* |
| 31 | + |
| 32 | + _ => return Err(IronError::new(Nope::ResourceNotFound, Status::NotFound)), |
| 33 | + }; |
| 34 | + |
| 35 | + // Set the cache times |
| 36 | + let mut response = Response::with((Status::Ok, contents)); |
| 37 | + let cache = vec![ |
| 38 | + CacheDirective::Public, |
| 39 | + CacheDirective::MaxAge(super::STATIC_FILE_CACHE_DURATION as u32), |
| 40 | + ]; |
| 41 | + response.headers.set(content_type); |
| 42 | + response.headers.set(CacheControl(cache)); |
| 43 | + |
| 44 | + Ok(response) |
| 45 | + } |
| 46 | + |
| 47 | + // Test each static route we serve |
| 48 | + #[test] |
| 49 | + fn serve_static_files() { |
| 50 | + $crate::test::wrapper(|env| { |
| 51 | + let web = env.frontend(); |
| 52 | + |
| 53 | + $( |
| 54 | + assert!( |
| 55 | + web.get(concat!("/-/static/", $file_name)).send()?.status().is_success(), |
| 56 | + concat!("failed while requesting a static file: '/-/static/", $file_name, "'"), |
| 57 | + ); |
| 58 | + )* |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + }); |
| 62 | + } |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +// Serve all of our vendor font & svg files |
| 67 | +serve_static_files! { |
| 68 | + "fa-brands-400.eot" => EOT, |
| 69 | + "fa-brands-400.ttf" => TTF, |
| 70 | + "fa-brands-400.woff2" => WOFF2, |
| 71 | + "fa-regular-400.svg" => SVG, |
| 72 | + "fa-regular-400.woff" => WOFF, |
| 73 | + "fa-solid-900.eot" => EOT, |
| 74 | + "fa-solid-900.ttf" => TTF, |
| 75 | + "fa-solid-900.woff2" => WOFF2, |
| 76 | + "fa-brands-400.svg" => SVG, |
| 77 | + "fa-brands-400.woff" => WOFF, |
| 78 | + "fa-regular-400.eot" => EOT, |
| 79 | + "fa-regular-400.ttf" => TTF, |
| 80 | + "fa-regular-400.woff2" => WOFF2, |
| 81 | + "fa-solid-900.svg" => SVG, |
| 82 | + "fa-solid-900.woff" => WOFF, |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn static_file_that_doesnt_exist() { |
| 87 | + crate::test::wrapper(|env| { |
| 88 | + let web = env.frontend(); |
| 89 | + assert!(web |
| 90 | + .get("/-/static/whoop-de-do.png") |
| 91 | + .send()? |
| 92 | + .status() |
| 93 | + .is_client_error()); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + }); |
| 97 | +} |
0 commit comments