Add /index.html and /image.html

This commit is contained in:
Joscha 2024-03-10 00:52:49 +01:00
parent 3cec866ba2
commit a51f3fa9b5
7 changed files with 241 additions and 1 deletions

View file

@ -0,0 +1,28 @@
use axum::{
http::{header, StatusCode, Uri},
response::{IntoResponse, Response},
};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "static"]
struct StaticFiles;
struct StaticFile(pub String);
impl IntoResponse for StaticFile {
fn into_response(self) -> Response {
match StaticFiles::get(&self.0) {
None => (StatusCode::NOT_FOUND, "404 Not Found").into_response(),
Some(file) => {
let mime = mime_guess::from_path(self.0).first_or_octet_stream();
([(header::CONTENT_TYPE, mime.as_ref())], file.data).into_response()
}
}
}
}
pub async fn get_static_file(uri: Uri) -> impl IntoResponse {
let path = uri.path().trim_start_matches('/').to_string();
StaticFile(path)
}