From dc3f6471e774eaafab92dfcd73dd089707646469 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 8 Jun 2022 14:58:29 -0400 Subject: Add hero image support with Also lightens the JavaScript load and shifts ad impressions to only when people from Reddit and Hacker News visit. I may have this include Twitter in the future. Signed-off-by: Xe --- src/handlers/blog.rs | 13 +++++++++++-- src/handlers/mod.rs | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/handlers') diff --git a/src/handlers/blog.rs b/src/handlers/blog.rs index f6aae06..6c66c97 100644 --- a/src/handlers/blog.rs +++ b/src/handlers/blog.rs @@ -4,6 +4,7 @@ use axum::{ extract::{Extension, Path}, response::Html, }; +use http::HeaderMap; use lazy_static::lazy_static; use prometheus::{opts, register_int_counter_vec, IntCounterVec}; use std::sync::Arc; @@ -72,10 +73,11 @@ pub async fn series_view( Ok(Html(result)) } -#[instrument(skip(state))] +#[instrument(skip(state, headers))] pub async fn post_view( Path(name): Path, Extension(state): Extension>, + headers: HeaderMap, ) -> Result { let mut want: Option = None; @@ -85,6 +87,13 @@ pub async fn post_view( } } + let referer = if let Some(referer) = headers.get(http::header::REFERER) { + let referer = referer.to_str()?.to_string(); + Some(referer) + } else { + None + }; + match want { None => Err(super::Error::PostNotFound(name)), Some(post) => { @@ -93,7 +102,7 @@ pub async fn post_view( .inc(); let body = templates::Html(post.body_html.clone()); let mut result: Vec = vec![]; - templates::blogpost_html(&mut result, post, body)?; + templates::blogpost_html(&mut result, post, body, referer)?; Ok(Html(result)) } } diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 97e0cb2..fa8203c 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -130,6 +130,9 @@ pub enum Error { #[error("axum http error: {0}")] AxumHTTP(#[from] axum::http::Error), + + #[error("string conversion error: {0}")] + ToStr(#[from] http::header::ToStrError), } pub type Result>> = std::result::Result; -- cgit v1.2.3