index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / routes / static.rs
-rw-r--r-- blob 0b4fe73dba3868e742580f8e88e9a114b59fd743 704 bytes (plain)
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
}