handlers: add Last-Modified support

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-03-13 08:51:31 -05:00
parent c1e6f535cf
commit 4f6c7c3716
5 changed files with 73 additions and 25 deletions

View File

@ -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<State>) -> Result<impl Reply, Rejection> {
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<State>) -> Result<impl Reply, Rejection> {
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<State>) -> Result<impl Reply
error!("series not found");
Err(SeriesNotFound(series).into())
} else {
Response::builder().html(|o| templates::series_posts_html(o, series, &posts))
Response::builder()
.header("Last-Modified", &*LAST_MODIFIED)
.html(|o| templates::series_posts_html(o, series, &posts))
}
}
@ -81,7 +87,9 @@ pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Re
.with_label_values(&[name.clone().as_str()])
.inc();
let body = Html(post.body_html.clone());
Response::builder().html(|o| templates::blogpost_html(o, post, body))
Response::builder()
.header("Last-Modified", &*LAST_MODIFIED)
.html(|o| templates::blogpost_html(o, post, body))
}
}
}

View File

@ -1,3 +1,4 @@
use super::LAST_MODIFIED;
use crate::{app::State, templates};
use lazy_static::lazy_static;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
@ -6,7 +7,7 @@ use tracing::instrument;
use warp::{http::Response, Rejection, Reply};
lazy_static! {
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
pub static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
opts!("feed_hits", "Number of hits to various feeds"),
&["kind"]
)
@ -56,6 +57,7 @@ pub async fn atom(state: Arc<State>, since: Option<String>) -> Result<impl Reply
.status(200)
.header("Content-Type", "application/atom+xml")
.header("ETag", ETAG.clone())
.header("Last-Modified", &*LAST_MODIFIED)
.body(buf)
.map_err(RenderError::Build)
.map_err(warp::reject::custom)
@ -88,6 +90,7 @@ pub async fn rss(state: Arc<State>, since: Option<String>) -> Result<impl Reply,
.status(200)
.header("Content-Type", "application/rss+xml")
.header("ETag", ETAG.clone())
.header("Last-Modified", &*LAST_MODIFIED)
.body(buf)
.map_err(RenderError::Build)
.map_err(warp::reject::custom)
@ -100,6 +103,7 @@ pub async fn sitemap(state: Arc<State>) -> Result<impl Reply, Rejection> {
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)

View File

@ -5,14 +5,16 @@ 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"])
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
opts!("gallery_hits", "Number of hits to gallery images"),
&["name"]
)
.unwrap();
}
@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Re
match want {
None => 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))
}

View File

@ -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<impl Reply, Rejection> {
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<impl Reply, Rejection> {
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<impl Reply, Rejection> {
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<State>) -> Result<impl Reply, Rejection> {
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<State>) -> Result<impl Reply, Rejection> {
"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<State>) -> Result<impl Reply, Rejection> {
pub async fn signalboost(state: Arc<State>) -> Result<impl Reply, Rejection> {
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<impl Reply, Rejection> {
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;

View File

@ -5,14 +5,16 @@ 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"])
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
opts!("talks_hits", "Number of hits to talks images"),
&["name"]
)
.unwrap();
}
@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Re
match want {
None => 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))
}