From 1c7717ab10364afbbe4225812409242f5076f4f8 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 13 Jun 2024 09:49:40 -0300 Subject: [PATCH] Serving static files (sorta kinda, still need to check the type) --- Cargo.lock | 64 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 7 +++-- src/staticfiles/mod.rs | 26 +++++++++++++++++ staticfiles/main.css | 5 ++++ templates/index.html | 1 + 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/staticfiles/mod.rs create mode 100644 staticfiles/main.css diff --git a/Cargo.lock b/Cargo.lock index 5d9840a..32f8c2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -526,6 +526,7 @@ dependencies = [ "askama", "axum", "clap", + "phf", "tokio", ] @@ -569,6 +570,48 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_macros", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e4cc64c2ad9ebe670cb8fd69dd50ae301650392e81c05f9bfcb2d5bdbc24b0" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3444646e286606587e49f3bcf1679b8cef1dc2c5ecc29ddacaffc305180d464b" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -619,6 +662,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -690,6 +748,12 @@ dependencies = [ "serde", ] +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "smallvec" version = "1.13.2" diff --git a/Cargo.toml b/Cargo.toml index 2062c24..7d89c5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" askama = "0.12.1" axum = "0.7.5" clap = { version = "4.5.7", features = ["derive", "env"] } +phf = { version = "0.11.2", features = ["macros"] } tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros"] } diff --git a/src/main.rs b/src/main.rs index dc9491c..6741dbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -mod index; mod args; +mod index; +mod staticfiles; use axum::routing::get; use axum::Router; @@ -12,7 +13,9 @@ async fn main() { println!("Starting server at {:?}", &opts.address); - let app = Router::new().route("/", get(index::index)); + let app = Router::new() + .route("/", get(index::index)) + .route("/static/:file", get(staticfiles::staticfile)); let listener = tokio::net::TcpListener::bind(&opts.address).await.unwrap(); axum::serve(listener, app).await.unwrap(); } diff --git a/src/staticfiles/mod.rs b/src/staticfiles/mod.rs new file mode 100644 index 0000000..a1777b6 --- /dev/null +++ b/src/staticfiles/mod.rs @@ -0,0 +1,26 @@ +//! Static file serving + +use axum::body::Body; +use axum::extract::Path; +use axum::http::header; +use axum::http::StatusCode; +use axum::response::Response; +use phf::phf_map; + +static FILES: phf::Map<&'static str, &'static str> = phf_map! { + "main.css" => include_str!("../../staticfiles/main.css") +}; + +pub(super) async fn staticfile(Path(filename): Path) -> Response { + match FILES.get(&filename) { + Some(content) => Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, "text/css") + .body(Body::from(*content)) + .unwrap(), + None => Response::builder() + .status(StatusCode::NOT_FOUND) + .body(Body::from("Not found")) + .unwrap(), + } +} diff --git a/staticfiles/main.css b/staticfiles/main.css new file mode 100644 index 0000000..a791b29 --- /dev/null +++ b/staticfiles/main.css @@ -0,0 +1,5 @@ +/* main */ +body { + background-color: #14142E; + color: #ffffff; +} diff --git a/templates/index.html b/templates/index.html index f9f5c20..d973c5f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,6 +3,7 @@ NoteVine + {% block main %}