From 4f6c7c3716aa81d32d5e0af130d5d2ab98d48e56 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 13 Mar 2021 08:51:31 -0500 Subject: [PATCH] handlers: add Last-Modified support Signed-off-by: Christine Dodrill --- src/handlers/blog.rs | 18 +++++++++++++----- src/handlers/feeds.rs | 6 +++++- src/handlers/gallery.rs | 16 ++++++++++------ src/handlers/mod.rs | 42 ++++++++++++++++++++++++++++++++++------- src/handlers/talks.rs | 16 ++++++++++------ 5 files changed, 73 insertions(+), 25 deletions(-) diff --git a/src/handlers/blog.rs b/src/handlers/blog.rs index fbc0c24..007b8e0 100644 --- a/src/handlers/blog.rs +++ b/src/handlers/blog.rs @@ -1,4 +1,4 @@ -use super::{PostNotFound, SeriesNotFound}; +use super::{PostNotFound, SeriesNotFound, LAST_MODIFIED}; use crate::{ app::State, post::Post, @@ -21,7 +21,9 @@ lazy_static! { #[instrument(skip(state))] pub async fn index(state: Arc) -> Result { let state = state.clone(); - Response::builder().html(|o| templates::blogindex_html(o, state.blog.clone())) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::blogindex_html(o, state.blog.clone())) } #[instrument(skip(state))] @@ -38,7 +40,9 @@ pub async fn series(state: Arc) -> Result { series.sort(); series.dedup(); - Response::builder().html(|o| templates::series_html(o, series)) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::series_html(o, series)) } #[instrument(skip(state))] @@ -60,7 +64,9 @@ pub async fn series_view(series: String, state: Arc) -> Result) -> Result, since: Option) -> Result, since: Option) -> Result) -> Result { Response::builder() .status(200) .header("Content-Type", "application/xml") + .header("Last-Modified", &*LAST_MODIFIED) .body(state.sitemap.clone()) .map_err(RenderError::Build) .map_err(warp::reject::custom) diff --git a/src/handlers/gallery.rs b/src/handlers/gallery.rs index ba3cba2..02bc01b 100644 --- a/src/handlers/gallery.rs +++ b/src/handlers/gallery.rs @@ -5,15 +5,17 @@ use crate::{ templates::{self, Html, RenderRucte}, }; use lazy_static::lazy_static; -use prometheus::{IntCounterVec, register_int_counter_vec, opts}; +use prometheus::{opts, register_int_counter_vec, IntCounterVec}; use std::sync::Arc; -use warp::{http::Response, Rejection, Reply}; use tracing::instrument; +use warp::{http::Response, Rejection, Reply}; lazy_static! { - static ref HIT_COUNTER: IntCounterVec = - register_int_counter_vec!(opts!("gallery_hits", "Number of hits to gallery images"), &["name"]) - .unwrap(); + static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!( + opts!("gallery_hits", "Number of hits to gallery images"), + &["name"] + ) + .unwrap(); } #[instrument(skip(state))] @@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc) -> Result Err(PostNotFound("gallery".into(), name).into()), Some(post) => { - HIT_COUNTER.with_label_values(&[name.clone().as_str()]).inc(); + HIT_COUNTER + .with_label_values(&[name.clone().as_str()]) + .inc(); let body = Html(post.body_html.clone()); Response::builder().html(|o| templates::gallerypost_html(o, post, body)) } diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index c6dbd65..798a911 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -2,6 +2,7 @@ use crate::{ app::State, templates::{self, Html, RenderRucte}, }; +use chrono::{Datelike, Timelike, Utc}; use lazy_static::lazy_static; use prometheus::{opts, register_int_counter_vec, IntCounterVec}; use std::{convert::Infallible, fmt, sync::Arc}; @@ -15,31 +16,52 @@ lazy_static! { static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"]) .unwrap(); + pub static ref LAST_MODIFIED: String = { + let now = Utc::now(); + format!( + "{dayname}, {day} {month} {year} {hour}:{minute}:{second} GMT", + dayname = now.weekday(), + day = now.day(), + month = now.month(), + year = now.year(), + hour = now.hour(), + minute = now.minute(), + second = now.second() + ) + }; } #[instrument] pub async fn index() -> Result { HIT_COUNTER.with_label_values(&["index"]).inc(); - Response::builder().html(|o| templates::index_html(o)) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::index_html(o)) } #[instrument] pub async fn contact() -> Result { HIT_COUNTER.with_label_values(&["contact"]).inc(); - Response::builder().html(|o| templates::contact_html(o)) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::contact_html(o)) } #[instrument] pub async fn feeds() -> Result { HIT_COUNTER.with_label_values(&["feeds"]).inc(); - Response::builder().html(|o| templates::feeds_html(o)) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::feeds_html(o)) } #[instrument(skip(state))] pub async fn resume(state: Arc) -> Result { HIT_COUNTER.with_label_values(&["resume"]).inc(); let state = state.clone(); - Response::builder().html(|o| templates::resume_html(o, Html(state.resume.clone()))) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::resume_html(o, Html(state.resume.clone()))) } #[instrument(skip(state))] @@ -53,7 +75,9 @@ pub async fn patrons(state: Arc) -> Result { "Could not load patrons, let me know the API token expired again".to_string(), ) }), - Some(patrons) => Response::builder().html(|o| templates::patrons_html(o, patrons.clone())), + Some(patrons) => Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::patrons_html(o, patrons.clone())), } } @@ -61,13 +85,17 @@ pub async fn patrons(state: Arc) -> Result { pub async fn signalboost(state: Arc) -> Result { HIT_COUNTER.with_label_values(&["signalboost"]).inc(); let state = state.clone(); - Response::builder().html(|o| templates::signalboost_html(o, state.signalboost.clone())) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::signalboost_html(o, state.signalboost.clone())) } #[instrument] pub async fn not_found() -> Result { HIT_COUNTER.with_label_values(&["not_found"]).inc(); - Response::builder().html(|o| templates::notfound_html(o, "some path".into())) + Response::builder() + .header("Last-Modified", &*LAST_MODIFIED) + .html(|o| templates::notfound_html(o, "some path".into())) } pub mod blog; diff --git a/src/handlers/talks.rs b/src/handlers/talks.rs index b64575c..8db5c9e 100644 --- a/src/handlers/talks.rs +++ b/src/handlers/talks.rs @@ -5,15 +5,17 @@ use crate::{ templates::{self, Html, RenderRucte}, }; use lazy_static::lazy_static; -use prometheus::{IntCounterVec, register_int_counter_vec, opts}; +use prometheus::{opts, register_int_counter_vec, IntCounterVec}; use std::sync::Arc; -use warp::{http::Response, Rejection, Reply}; use tracing::instrument; +use warp::{http::Response, Rejection, Reply}; lazy_static! { - static ref HIT_COUNTER: IntCounterVec = - register_int_counter_vec!(opts!("talks_hits", "Number of hits to talks images"), &["name"]) - .unwrap(); + static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!( + opts!("talks_hits", "Number of hits to talks images"), + &["name"] + ) + .unwrap(); } #[instrument(skip(state))] @@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc) -> Result Err(PostNotFound("talks".into(), name).into()), Some(post) => { - HIT_COUNTER.with_label_values(&[name.clone().as_str()]).inc(); + HIT_COUNTER + .with_label_values(&[name.clone().as_str()]) + .inc(); let body = Html(post.body_html.clone()); Response::builder().html(|o| templates::talkpost_html(o, post, body)) }