From 6367cafa19ccf66f73f45a446f4302c2ab22300d Mon Sep 17 00:00:00 2001 From: Chris Rose Date: Mon, 2 Oct 2023 23:20:06 -0700 Subject: [PATCH 1/2] A man is not dead while his name is still spoken - implement the GNU Terry Pratchet clacks overhead: http://gnuterrypratchett.com/ --- atuin-server/src/router.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/atuin-server/src/router.rs b/atuin-server/src/router.rs index 7cfcdad1af9..7d8eed571da 100644 --- a/atuin-server/src/router.rs +++ b/atuin-server/src/router.rs @@ -2,7 +2,9 @@ use async_trait::async_trait; use atuin_common::api::ErrorResponse; use axum::{ extract::FromRequestParts, - response::IntoResponse, + http::Request, + middleware::Next, + response::{IntoResponse, Response}, routing::{delete, get, post}, Router, }; @@ -76,6 +78,16 @@ async fn teapot() -> impl IntoResponse { (http::StatusCode::IM_A_TEAPOT, "🫖") } +async fn clacks_overhead(request: Request, next: Next) -> Response { + let mut response = next.run(request).await; + + let gnu_terry_value = "GNU Terry Pratchett"; + let gnu_terry_header = "X-Clacks-Overhead"; + + response.headers_mut().insert(gnu_terry_header, gnu_terry_value.parse().unwrap()); + response +} + #[derive(Clone)] pub struct AppState { pub database: DB, @@ -107,5 +119,7 @@ pub fn router(database: DB, settings: Settings) -> R } .fallback(teapot) .with_state(AppState { database, settings }) - .layer(ServiceBuilder::new().layer(TraceLayer::new_for_http())) + .layer(ServiceBuilder::new() + .layer(axum::middleware::from_fn(clacks_overhead)) + .layer(TraceLayer::new_for_http())) } From 21810f5d76e366940a93bd214102153b7030059f Mon Sep 17 00:00:00 2001 From: Chris Rose Date: Tue, 3 Oct 2023 12:24:13 -0700 Subject: [PATCH 2/2] Fix up formatting --- atuin-server/src/router.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/atuin-server/src/router.rs b/atuin-server/src/router.rs index 7d8eed571da..0100822e935 100644 --- a/atuin-server/src/router.rs +++ b/atuin-server/src/router.rs @@ -84,7 +84,9 @@ async fn clacks_overhead(request: Request, next: Next) -> Response { let gnu_terry_value = "GNU Terry Pratchett"; let gnu_terry_header = "X-Clacks-Overhead"; - response.headers_mut().insert(gnu_terry_header, gnu_terry_value.parse().unwrap()); + response + .headers_mut() + .insert(gnu_terry_header, gnu_terry_value.parse().unwrap()); response } @@ -119,7 +121,9 @@ pub fn router(database: DB, settings: Settings) -> R } .fallback(teapot) .with_state(AppState { database, settings }) - .layer(ServiceBuilder::new() - .layer(axum::middleware::from_fn(clacks_overhead)) - .layer(TraceLayer::new_for_http())) + .layer( + ServiceBuilder::new() + .layer(axum::middleware::from_fn(clacks_overhead)) + .layer(TraceLayer::new_for_http()), + ) }