From 701350a2695eb0e3db4636ab9fd6a4d1aab46d54 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 2 Jan 2025 19:19:39 -0500 Subject: [PATCH] Use hyper instead of axum --- Cargo.lock | 108 ++-------------------------------------------------- Cargo.toml | 4 +- src/main.rs | 42 +++++++++++++------- 3 files changed, 34 insertions(+), 120 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d12215..969a94e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,60 +120,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" -[[package]] -name = "axum" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d6fd624c75e18b3b4c6b9caf42b1afe24437daaee904069137d8bab077be8b8" -dependencies = [ - "axum-core", - "bytes", - "form_urlencoded", - "futures-util", - "http", - "http-body", - "http-body-util", - "hyper", - "hyper-util", - "itoa", - "matchit", - "memchr", - "mime", - "percent-encoding", - "pin-project-lite", - "rustversion", - "serde", - "serde_json", - "serde_path_to_error", - "serde_urlencoded", - "sync_wrapper", - "tokio", - "tower", - "tower-layer", - "tower-service", - "tracing", -] - -[[package]] -name = "axum-core" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df1362f362fd16024ae199c1970ce98f9661bf5ef94b9808fee734bc3698b733" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "http-body-util", - "mime", - "pin-project-lite", - "rustversion", - "sync_wrapper", - "tower-layer", - "tower-service", - "tracing", -] - [[package]] name = "backtrace" version = "0.3.74" @@ -1186,12 +1132,6 @@ dependencies = [ "xml5ever", ] -[[package]] -name = "matchit" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" - [[package]] name = "memchr" version = "2.7.4" @@ -1595,12 +1535,6 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" -[[package]] -name = "rustversion" -version = "1.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" - [[package]] name = "ryu" version = "1.0.18" @@ -1654,16 +1588,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_path_to_error" -version = "0.1.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6" -dependencies = [ - "itoa", - "serde", -] - [[package]] name = "serde_spanned" version = "0.6.8" @@ -1673,18 +1597,6 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_urlencoded" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" -dependencies = [ - "form_urlencoded", - "itoa", - "ryu", - "serde", -] - [[package]] name = "sha2" version = "0.10.8" @@ -1801,12 +1713,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "sync_wrapper" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" - [[package]] name = "synstructure" version = "0.13.1" @@ -1989,14 +1895,8 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ - "futures-core", - "futures-util", - "pin-project-lite", - "sync_wrapper", - "tokio", "tower-layer", "tower-service", - "tracing", ] [[package]] @@ -2042,7 +1942,6 @@ version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ - "log", "pin-project-lite", "tracing-core", ] @@ -2052,9 +1951,6 @@ name = "tracing-core" version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" -dependencies = [ - "once_cell", -] [[package]] name = "typenum" @@ -2185,7 +2081,6 @@ name = "v7" version = "0.1.0" dependencies = [ "anyhow", - "axum", "base64", "chrono", "clap", @@ -2196,6 +2091,8 @@ dependencies = [ "grass", "grass_compiler", "html5ever", + "hyper", + "hyper-util", "log", "markup5ever_rcdom", "notify", @@ -2208,6 +2105,7 @@ dependencies = [ "tokio", "tokio-stream", "toml", + "tower", "tower-http", "unicode-normalization", "url", diff --git a/Cargo.toml b/Cargo.toml index a11ab77..e631834 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ serde_json = "1.0" [dependencies] anyhow = "1.0.95" -axum = "0.8.1" base64 = "0.22.1" chrono = { version = "0.4.39", features = ["serde"] } clap = { version = "4.5.23", features = ["cargo"] } @@ -30,6 +29,8 @@ grass_compiler = { version = "0.13.4", features = [ "custom-builtin-fns", ], git = "https://git.shadowfacts.net/shadowfacts/grass.git", branch = "custom-global-variables" } html5ever = "0.27.0" +hyper = { version = "1.5.2", features = ["server", "http1"] } +hyper-util = { version = "0.1.10", features = ["tokio", "service"] } log = "0.4.22" markup5ever_rcdom = "0.3.0" notify = "7.0.0" @@ -41,6 +42,7 @@ tera = "1.20.0" tokio = { version = "1.42.0", features = ["full"] } tokio-stream = "0.1.17" toml = "0.8.19" +tower = "0.5.2" tower-http = { version = "0.6.2", features = ["fs"] } unicode-normalization = "0.1.24" url = "2.5.4" diff --git a/src/main.rs b/src/main.rs index c353242..833627f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,16 @@ mod generator; -use axum::Router; use clap::{Arg, ArgAction, ArgMatches, Command, command}; use compute_graph::AsyncGraph; use debounced::debounced; use futures::join; use generator::{FileWatcher, content_base_path}; -use log::info; +use hyper::server::conn::http1; +use hyper_util::{rt::TokioIo, service::TowerToHyperService}; +use log::{error, info}; use std::cell::RefCell; +use std::net::SocketAddr; use std::rc::Rc; use std::time::Duration; use tokio::net::TcpListener; @@ -64,18 +66,30 @@ async fn main() { async fn serve(matches: &ArgMatches) { if let Some(("serve", _)) = matches.subcommand() { - // TODO: consider not pulling in all of axum just for this - let app = Router::new().fallback_service( - ServeDir::new("out/").not_found_service(ServeFile::new("out/404.html")), - ); - let addr = if cfg!(debug_assertions) { - "0.0.0.0:8084" - } else { - "127.0.0.1:8084" - }; - info!("Listening on {}", addr); - let listener = TcpListener::bind(addr).await.unwrap(); - axum::serve(listener, app).await.unwrap(); + let service = ServeDir::new("out/").not_found_service(ServeFile::new("out/404.html")); + + let addr = SocketAddr::from(( + if cfg!(debug_assertions) { + [0, 0, 0, 0] + } else { + [127, 0, 0, 1] + }, + 8084, + )); + info!("Listening on {addr}"); + + let listener = TcpListener::bind(addr).await.expect("binding to tcp port"); + loop { + let (tcp, _) = listener.accept().await.expect("accepting connection"); + let io = TokioIo::new(tcp); + let service = TowerToHyperService::new(service.clone()); + tokio::task::spawn(async move { + let result = http1::Builder::new().serve_connection(io, service).await; + if let Err(e) = result { + error!("Error handling connection: {e:?}"); + } + }); + } } }