index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/routes/static.rs')

-rw-r--r-- src/routes/static.rs 26
 1 file changed, 26 insertions(+)

diff --git a/src/routes/static.rs b/src/routes/static.rs
new file mode 100644
index 0000000..0b4fe73 100644
--- /dev/null
+++ b/src/routes/static.rs
@@ -1,0 +1,26 @@
use rust_embed::RustEmbed;

use crate::prelude::*;

#[derive(RustEmbed)]
#[folder = "static/"]
struct Static;

#[get("/static/{filename:.*}")]
pub async fn files(path: web::Path<String>) -> ActixResult<HttpResponse> {
    let path = path.into_inner();

    if path.is_empty() {
        return Ok(HttpResponse::NotFound().body("Not Found"));
    }

    match Static::get(&path) {
        Some(content) => {
            let mime_type = mime_guess::from_path(&path).first_or_octet_stream();
            Ok(HttpResponse::Ok()
                .content_type(mime_type.as_ref())
                .body(content.data.into_owned()))
        }
        None => Ok(HttpResponse::NotFound().body("Not Found")),
    }
}