From 41632f7fc649592100a43d9de5d38eb102301e43 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 4 Oct 2023 23:11:36 +0200 Subject: [PATCH] Clean up fs access --- Cargo.lock | 32 +++++++++++++++++++++++++++++--- Cargo.toml | 1 + src/main.rs | 23 ++++------------------- static/template.html | 4 ++-- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85b988a..2f04f15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1154,6 +1154,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "mio" version = "0.8.6" @@ -1737,6 +1747,7 @@ dependencies = [ "shuttle-runtime", "sync_wrapper", "tokio", + "tower-http", "url", ] @@ -2686,18 +2697,24 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.0" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d1d42a9b3f3ec46ba828e8d376aec14592ea199f70a06a548587ecd1c4ab658" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.4.0", "bytes", "futures-core", "futures-util", "http", "http-body", "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", "pin-project-lite", + "tokio", + "tokio-util", "tower-layer", "tower-service", "tracing", @@ -2823,6 +2840,15 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.13" diff --git a/Cargo.toml b/Cargo.toml index 86443e2..35d6711 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ kuchiki = "0.8.1" shuttle-axum = { version = "0.28.0" } shuttle-runtime = { version = "0.28.0" } once_cell = "1.17.1" +tower-http = { version = "0.4.4", features = ["fs"] } diff --git a/src/main.rs b/src/main.rs index 7d13447..718f5b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,16 @@ +use std::path::PathBuf; + use axum::{ body::Body, extract::TypedHeader, headers::UserAgent, http::{HeaderValue, StatusCode, Uri}, response::{self, Html, IntoResponse, Response}, - routing::get, Router, }; use readable_readability::Readability; use reqwest::header::{CONTENT_TYPE, USER_AGENT}; +use tower_http::services::ServeDir; mod utils; @@ -161,24 +163,7 @@ pub fn static_content( #[shuttle_runtime::main] async fn axum() -> shuttle_axum::ShuttleAxum { let router = Router::new() - .route( - "/static/Crimson.woff2", - get(|| async { - static_content( - include_bytes!("../static/fonts/Crimson.woff2",), - HeaderValue::from_static("text/woff2"), - ) - }), - ) - .route( - "/static/JetBrainsMono.woff2", - get(|| async { - static_content( - include_bytes!("../static/fonts/JetBrainsMono.woff2",), - HeaderValue::from_static("font/woff2"), - ) - }), - ) + .nest_service("/static", ServeDir::new(PathBuf::from("static"))) .fallback(readable); Ok(router.into()) diff --git a/static/template.html b/static/template.html index ea5cb14..5ef0f07 100644 --- a/static/template.html +++ b/static/template.html @@ -15,12 +15,12 @@ @font-face { font-family: "Crimson"; - src: url(/static/Crimson.woff2); + src: url(/static/fonts/Crimson.woff2); } @font-face { font-family: "JetBrainsMono"; - src: url(/static/JetBrainsMono.woff2); + src: url(/static/fonts/JetBrainsMono.woff2); } * {