Replace mime_guess dep with a simple function

This commit is contained in:
Marcin Kulik
2025-05-11 20:28:31 +02:00
parent 76ac4be5d7
commit 22cd6eead3
3 changed files with 18 additions and 4 deletions

1
Cargo.lock generated
View File

@@ -94,7 +94,6 @@ dependencies = [
"clap_mangen",
"config",
"futures-util",
"mime_guess",
"nix 0.30.1",
"reqwest",
"rgb",

View File

@@ -34,7 +34,6 @@ tokio = { version = "1.44.2", features = ["full"] }
futures-util = "0.3.31"
tokio-stream = { version = "0.1.17", features = ["sync"] }
rust-embed = "8.2.0"
mime_guess = "2.0.4"
tower-http = { version = "0.6.2", features = ["trace"] }
tracing = "0.1.41"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

View File

@@ -1,6 +1,7 @@
use std::future;
use std::io;
use std::net::SocketAddr;
use std::path::Path;
use axum::extract::connect_info::ConnectInfo;
use axum::extract::ws::{self, CloseCode, CloseFrame, Message, WebSocket, WebSocketUpgrade};
@@ -71,15 +72,30 @@ async fn static_handler(uri: Uri) -> impl IntoResponse {
match Assets::get(path) {
Some(content) => {
let mime = mime_guess::from_path(path).first_or_octet_stream();
let mime = mime_from_path(path);
([(header::CONTENT_TYPE, mime.as_ref())], content.data).into_response()
([(header::CONTENT_TYPE, mime)], content.data).into_response()
}
None => (StatusCode::NOT_FOUND, "404").into_response(),
}
}
fn mime_from_path(path: &str) -> &str {
let lowercase_path = &path.to_lowercase();
let ext = Path::new(lowercase_path)
.extension()
.and_then(|e| e.to_str());
match ext {
Some("html") => "text/html",
Some("js") => "text/javascript",
Some("css") => "text/css",
Some(_) | None => "application/octet-stream",
}
}
async fn ws_handler(
ws: WebSocketUpgrade,
ConnectInfo(addr): ConnectInfo<SocketAddr>,