handlers: add Last-Modified support
Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
parent
c1e6f535cf
commit
4f6c7c3716
|
@ -1,4 +1,4 @@
|
||||||
use super::{PostNotFound, SeriesNotFound};
|
use super::{PostNotFound, SeriesNotFound, LAST_MODIFIED};
|
||||||
use crate::{
|
use crate::{
|
||||||
app::State,
|
app::State,
|
||||||
post::Post,
|
post::Post,
|
||||||
|
@ -21,7 +21,9 @@ lazy_static! {
|
||||||
#[instrument(skip(state))]
|
#[instrument(skip(state))]
|
||||||
pub async fn index(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
pub async fn index(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
||||||
let state = state.clone();
|
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))]
|
#[instrument(skip(state))]
|
||||||
|
@ -38,7 +40,9 @@ pub async fn series(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
||||||
series.sort();
|
series.sort();
|
||||||
series.dedup();
|
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))]
|
#[instrument(skip(state))]
|
||||||
|
@ -60,7 +64,9 @@ pub async fn series_view(series: String, state: Arc<State>) -> Result<impl Reply
|
||||||
error!("series not found");
|
error!("series not found");
|
||||||
Err(SeriesNotFound(series).into())
|
Err(SeriesNotFound(series).into())
|
||||||
} else {
|
} 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()])
|
.with_label_values(&[name.clone().as_str()])
|
||||||
.inc();
|
.inc();
|
||||||
let body = Html(post.body_html.clone());
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use super::LAST_MODIFIED;
|
||||||
use crate::{app::State, templates};
|
use crate::{app::State, templates};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
||||||
|
@ -6,7 +7,7 @@ use tracing::instrument;
|
||||||
use warp::{http::Response, Rejection, Reply};
|
use warp::{http::Response, Rejection, Reply};
|
||||||
|
|
||||||
lazy_static! {
|
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"),
|
opts!("feed_hits", "Number of hits to various feeds"),
|
||||||
&["kind"]
|
&["kind"]
|
||||||
)
|
)
|
||||||
|
@ -56,6 +57,7 @@ pub async fn atom(state: Arc<State>, since: Option<String>) -> Result<impl Reply
|
||||||
.status(200)
|
.status(200)
|
||||||
.header("Content-Type", "application/atom+xml")
|
.header("Content-Type", "application/atom+xml")
|
||||||
.header("ETag", ETAG.clone())
|
.header("ETag", ETAG.clone())
|
||||||
|
.header("Last-Modified", &*LAST_MODIFIED)
|
||||||
.body(buf)
|
.body(buf)
|
||||||
.map_err(RenderError::Build)
|
.map_err(RenderError::Build)
|
||||||
.map_err(warp::reject::custom)
|
.map_err(warp::reject::custom)
|
||||||
|
@ -88,6 +90,7 @@ pub async fn rss(state: Arc<State>, since: Option<String>) -> Result<impl Reply,
|
||||||
.status(200)
|
.status(200)
|
||||||
.header("Content-Type", "application/rss+xml")
|
.header("Content-Type", "application/rss+xml")
|
||||||
.header("ETag", ETAG.clone())
|
.header("ETag", ETAG.clone())
|
||||||
|
.header("Last-Modified", &*LAST_MODIFIED)
|
||||||
.body(buf)
|
.body(buf)
|
||||||
.map_err(RenderError::Build)
|
.map_err(RenderError::Build)
|
||||||
.map_err(warp::reject::custom)
|
.map_err(warp::reject::custom)
|
||||||
|
@ -100,6 +103,7 @@ pub async fn sitemap(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(200)
|
.status(200)
|
||||||
.header("Content-Type", "application/xml")
|
.header("Content-Type", "application/xml")
|
||||||
|
.header("Last-Modified", &*LAST_MODIFIED)
|
||||||
.body(state.sitemap.clone())
|
.body(state.sitemap.clone())
|
||||||
.map_err(RenderError::Build)
|
.map_err(RenderError::Build)
|
||||||
.map_err(warp::reject::custom)
|
.map_err(warp::reject::custom)
|
||||||
|
|
|
@ -5,14 +5,16 @@ use crate::{
|
||||||
templates::{self, Html, RenderRucte},
|
templates::{self, Html, RenderRucte},
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
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 std::sync::Arc;
|
||||||
use warp::{http::Response, Rejection, Reply};
|
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
|
use warp::{http::Response, Rejection, Reply};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref HIT_COUNTER: IntCounterVec =
|
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
|
||||||
register_int_counter_vec!(opts!("gallery_hits", "Number of hits to gallery images"), &["name"])
|
opts!("gallery_hits", "Number of hits to gallery images"),
|
||||||
|
&["name"]
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Re
|
||||||
match want {
|
match want {
|
||||||
None => Err(PostNotFound("gallery".into(), name).into()),
|
None => Err(PostNotFound("gallery".into(), name).into()),
|
||||||
Some(post) => {
|
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());
|
let body = Html(post.body_html.clone());
|
||||||
Response::builder().html(|o| templates::gallerypost_html(o, post, body))
|
Response::builder().html(|o| templates::gallerypost_html(o, post, body))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::{
|
||||||
app::State,
|
app::State,
|
||||||
templates::{self, Html, RenderRucte},
|
templates::{self, Html, RenderRucte},
|
||||||
};
|
};
|
||||||
|
use chrono::{Datelike, Timelike, Utc};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
||||||
use std::{convert::Infallible, fmt, sync::Arc};
|
use std::{convert::Infallible, fmt, sync::Arc};
|
||||||
|
@ -15,31 +16,52 @@ lazy_static! {
|
||||||
static ref HIT_COUNTER: IntCounterVec =
|
static ref HIT_COUNTER: IntCounterVec =
|
||||||
register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"])
|
register_int_counter_vec!(opts!("hits", "Number of hits to various pages"), &["page"])
|
||||||
.unwrap();
|
.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]
|
#[instrument]
|
||||||
pub async fn index() -> Result<impl Reply, Rejection> {
|
pub async fn index() -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["index"]).inc();
|
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]
|
#[instrument]
|
||||||
pub async fn contact() -> Result<impl Reply, Rejection> {
|
pub async fn contact() -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["contact"]).inc();
|
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]
|
#[instrument]
|
||||||
pub async fn feeds() -> Result<impl Reply, Rejection> {
|
pub async fn feeds() -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["feeds"]).inc();
|
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))]
|
#[instrument(skip(state))]
|
||||||
pub async fn resume(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
pub async fn resume(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["resume"]).inc();
|
HIT_COUNTER.with_label_values(&["resume"]).inc();
|
||||||
let state = state.clone();
|
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))]
|
#[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(),
|
"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> {
|
pub async fn signalboost(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["signalboost"]).inc();
|
HIT_COUNTER.with_label_values(&["signalboost"]).inc();
|
||||||
let state = state.clone();
|
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]
|
#[instrument]
|
||||||
pub async fn not_found() -> Result<impl Reply, Rejection> {
|
pub async fn not_found() -> Result<impl Reply, Rejection> {
|
||||||
HIT_COUNTER.with_label_values(&["not_found"]).inc();
|
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;
|
pub mod blog;
|
||||||
|
|
|
@ -5,14 +5,16 @@ use crate::{
|
||||||
templates::{self, Html, RenderRucte},
|
templates::{self, Html, RenderRucte},
|
||||||
};
|
};
|
||||||
use lazy_static::lazy_static;
|
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 std::sync::Arc;
|
||||||
use warp::{http::Response, Rejection, Reply};
|
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
|
use warp::{http::Response, Rejection, Reply};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref HIT_COUNTER: IntCounterVec =
|
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
|
||||||
register_int_counter_vec!(opts!("talks_hits", "Number of hits to talks images"), &["name"])
|
opts!("talks_hits", "Number of hits to talks images"),
|
||||||
|
&["name"]
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +37,9 @@ pub async fn post_view(name: String, state: Arc<State>) -> Result<impl Reply, Re
|
||||||
match want {
|
match want {
|
||||||
None => Err(PostNotFound("talks".into(), name).into()),
|
None => Err(PostNotFound("talks".into(), name).into()),
|
||||||
Some(post) => {
|
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());
|
let body = Html(post.body_html.clone());
|
||||||
Response::builder().html(|o| templates::talkpost_html(o, post, body))
|
Response::builder().html(|o| templates::talkpost_html(o, post, body))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue