| 1 | use rust_embed::RustEmbed; |
| 2 | |
| 3 | use crate::prelude::*; |
| 4 | |
| 5 | #[derive(RustEmbed)] |
| 6 | #[folder = "static/"] |
| 7 | struct Static; |
| 8 | |
| 9 | #[get("/static/{filename:.*}")] |
| 10 | pub async fn files(path: web::Path<String>) -> ActixResult<HttpResponse> { |
| 11 | let path = path.into_inner(); |
| 12 | |
| 13 | if path.is_empty() { |
| 14 | return Ok(HttpResponse::NotFound().body("Not Found")); |
| 15 | } |
| 16 | |
| 17 | match Static::get(&path) { |
| 18 | Some(content) => { |
| 19 | let mime_type = mime_guess::from_path(&path).first_or_octet_stream(); |
| 20 | Ok(HttpResponse::Ok() |
| 21 | .content_type(mime_type.as_ref()) |
| 22 | .body(content.data.into_owned())) |
| 23 | } |
| 24 | None => Ok(HttpResponse::NotFound().body("Not Found")), |
| 25 | } |
| 26 | } |
