Merge a few functions
Some checks failed
build / binary (push) Has been cancelled
build / binary-static (x86_64-linux) (push) Has been cancelled
build / container (push) Has been cancelled
build / clippy (push) Has been cancelled
lint / linting (push) Has been cancelled

There's no need to have `poison()`, `poison_root()`, and
`poison_path()`: it is enough to have one, and mark path as
`Option<Path<String>>`, and supply a default.

Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2025-02-24 22:46:48 +01:00
parent d8cc61e636
commit d291d508cb
No known key found for this signature in database

View file

@ -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<String> {
async fn poison(
headers: axum::http::HeaderMap,
State(iocaine): State<StatefulIocaine>,
path: Option<Path<String>>,
) -> Html<String> {
let default_host = axum::http::HeaderValue::from_static("<unknown>");
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<StatefulIocaine>,
) -> Html<String> {
poison(&iocaine, headers, "/")
}
async fn poison_path(
headers: axum::http::HeaderMap,
State(iocaine): State<StatefulIocaine>,
Path(path): Path<String>,
) -> Html<String> {
poison(&iocaine, headers, &path)
}