From d291d508cbfbc8babf76f04872c7b8d042246bc3 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 24 Feb 2025 22:46:48 +0100 Subject: [PATCH] Merge a few functions There's no need to have `poison()`, `poison_root()`, and `poison_path()`: it is enough to have one, and mark path as `Option>`, and supply a default. Signed-off-by: Gergely Nagy --- src/app.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index 322911a..27c479c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -48,8 +48,8 @@ impl Iocaine { let state: StatefulIocaine = Arc::new(self); let mut app = Router::new() - .route("/", get(poison_root)) - .route("/{*path}", get(poison_path)) + .route("/", get(poison)) + .route("/{*path}", get(poison)) .layer(tower_http::trace::TraceLayer::new_for_http()); if state.config.metrics.enable { @@ -106,15 +106,20 @@ pub async fn shutdown_signal() { } } -fn poison(iocaine: &StatefulIocaine, headers: axum::http::HeaderMap, path: &str) -> Html { +async fn poison( + headers: axum::http::HeaderMap, + State(iocaine): State, + path: Option>, +) -> Html { let default_host = axum::http::HeaderValue::from_static(""); let host = headers .get("host") .unwrap_or(&default_host) .to_str() .unwrap(); + let path = path.unwrap_or(Path("/".to_string())); - let garbage = AssembledStatisticalSequences::generate(iocaine, host, path); + let garbage = AssembledStatisticalSequences::generate(&iocaine, host, &path); if iocaine.config.metrics.enable { metrics::counter!("iocaine_garbage_served").increment(garbage.len() as u64); @@ -122,18 +127,3 @@ fn poison(iocaine: &StatefulIocaine, headers: axum::http::HeaderMap, path: &str) Html(garbage) } - -async fn poison_root( - headers: axum::http::HeaderMap, - State(iocaine): State, -) -> Html { - poison(&iocaine, headers, "/") -} - -async fn poison_path( - headers: axum::http::HeaderMap, - State(iocaine): State, - Path(path): Path, -) -> Html { - poison(&iocaine, headers, &path) -}