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 cb0e0b19f074fd99d136e351de83b18cc50a2ab0 783 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
                .insert_header(("Cache-Control", "no-cache, must-revalidate"))
23
                .body(content.data.into_owned()))
24
        }
25
        None => Ok(HttpResponse::NotFound().body("Not Found")),
26
    }
27
}