mirror of
https://git.madhouse-project.org/algernon/iocaine.git
synced 2025-03-10 09:18:49 +01:00
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<Path<String>>`, and supply a default. Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
This commit is contained in:
parent
d8cc61e636
commit
d291d508cb
1 changed files with 9 additions and 19 deletions
28
src/app.rs
28
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<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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue