From cc3825aacc94c7a572ed46900a01c8df9e008141 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Mon, 21 Mar 2022 15:17:11 +0000 Subject: fix headers Signed-off-by: Xe Iaso --- src/main.rs | 64 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index a39ab7f..c6dc33f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,12 @@ extern crate tracing; use axum::{ body, extract::Extension, - http::header, + http::header::{self, HeaderValue, CACHE_CONTROL, CONTENT_TYPE}, response::{Html, Response}, routing::get, Router, }; use color_eyre::eyre::Result; -use http::header::{CACHE_CONTROL, CONTENT_TYPE}; use hyper::StatusCode; use prometheus::{Encoder, TextEncoder}; use std::{ @@ -40,6 +39,28 @@ async fn healthcheck() -> &'static str { "OK" } +fn cache_header(_: &Response) -> Option { + Some(header::HeaderValue::from_static( + "public, max-age=3600, stale-if-error=60", + )) +} + +fn webmention_header(_: &Response) -> Option { + Some(header::HeaderValue::from_static( + r#"; rel="webmention""#, + )) +} + +fn clacks_header(_: &Response) -> Option { + Some(HeaderValue::from_static("Ashlynn")) +} + +fn hacker_header(_: &Response) -> Option { + Some(header::HeaderValue::from_static( + "If you are reading this, check out /signalboost to find people for your team", + )) +} + #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; @@ -59,28 +80,23 @@ async fn main() -> Result<()> { let middleware = tower::ServiceBuilder::new() .layer(TraceLayer::new_for_http()) - .layer(Extension(state.clone())); - // .layer(SetResponseHeaderLayer::overriding( - // header::CACHE_CONTROL, - // |_| header::HeaderValue::from_static("public, max-age=3600, stale-if-error=60"), - // )) - // .layer(SetResponseHeaderLayer::appending(header::LINK, |_| { - // header::HeaderValue::from_static( - // r#"; rel="webmention""#, - // ) - // })) - // .layer(SetResponseHeaderLayer::appending( - // header::HeaderName::from_static("x-clacks-overhead"), - // |_| header::HeaderValue::from_static("Ashlynn"), - // )) - // .layer(SetResponseHeaderLayer::overriding( - // header::HeaderName::from_static("x-hacker"), - // |_| { - // header::HeaderValue::from_static( - // "If you are reading this, check out /signalboost to find people for your team", - // ) - // }, - // )); + .layer(Extension(state.clone())) + .layer(SetResponseHeaderLayer::overriding( + header::CACHE_CONTROL, + cache_header, + )) + .layer(SetResponseHeaderLayer::appending( + header::LINK, + webmention_header, + )) + .layer(SetResponseHeaderLayer::appending( + header::HeaderName::from_static("x-clacks-overhead"), + clacks_header, + )) + .layer(SetResponseHeaderLayer::overriding( + header::HeaderName::from_static("x-hacker"), + hacker_header, + )); let app = Router::new() // meta -- cgit v1.2.3