From cac77accd19f067ee8b3b87deb367f7e3938de2a Mon Sep 17 00:00:00 2001 From: insects Date: Wed, 5 Feb 2025 15:15:19 +0100 Subject: [PATCH] add static files support; basic css --- Cargo.lock | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 2 ++ src/templates.rs | 1 + static/style.css | 3 +++ 5 files changed, 74 insertions(+) create mode 100644 static/style.css diff --git a/Cargo.lock b/Cargo.lock index a69e0e3..653c2a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -137,6 +137,7 @@ dependencies = [ "serde_json", "serde_path_to_error", "tokio", + "tower-http", ] [[package]] @@ -222,6 +223,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + [[package]] name = "futures-task" version = "0.3.31" @@ -280,6 +287,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9171a2ea8a68358193d15dd5d70c1c10a2afc3e7e4c5bc92bc9f025cebd7359c" + [[package]] name = "httparse" version = "1.10.0" @@ -430,6 +443,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "miniz_oxide" version = "0.8.3" @@ -709,6 +732,19 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7fcaa8d55a2bdd6b83ace262b016eca0d79ee02818c5c1bcdf0305114081078" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tower" version = "0.5.2" @@ -725,6 +761,31 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403fa3b783d4b626a8ad51d766ab03cb6d2dbfc46b1c5d4448395e6628dc9697" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-layer" version = "0.3.3" @@ -757,6 +818,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "unicase" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539" + [[package]] name = "unicode-ident" version = "1.0.16" diff --git a/Cargo.toml b/Cargo.toml index af76f86..b376be5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.138" serde_path_to_error = "0.1.16" tokio = { version = "1.43.0", features = ["full"] } +tower-http = { version = "0.6.2", features = ["fs"] } diff --git a/src/main.rs b/src/main.rs index 250fa28..8681eb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router}; use data::Data; use maud::Markup; +use tower_http::services::ServeDir; pub mod clock; pub mod data; @@ -43,6 +44,7 @@ async fn main_handler(state: State>) -> Result { async fn main() { let app = Router::new() .route("/", get(main_handler)) + .nest_service("/static", ServeDir::new("static")) .with_state(Arc::new(AppState { data: Data::new() })); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); println!("Listening on http://localhost:3000!"); diff --git a/src/templates.rs b/src/templates.rs index 0d67b86..8d8256f 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -16,6 +16,7 @@ pub fn layout(content: Markup) -> Markup { head { title { "beacon" } meta name="viewport" content="width=device-width"; + link rel="stylesheet" href="/static/style.css"; } body { (content) } diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..d224431 --- /dev/null +++ b/static/style.css @@ -0,0 +1,3 @@ +h1 { + color: red; +}