2020-07-27 03:12:01 +00:00
|
|
|
use crate::{app::State, templates};
|
2020-07-16 19:32:30 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
2020-07-27 03:12:01 +00:00
|
|
|
use std::{sync::Arc, io};
|
2020-07-16 19:32:30 +00:00
|
|
|
use warp::{http::Response, Rejection, Reply};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
|
|
|
|
opts!("feed_hits", "Number of hits to various feeds"),
|
|
|
|
&["kind"]
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn jsonfeed(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
|
|
|
HIT_COUNTER.with_label_values(&["json"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
Ok(warp::reply::json(&state.jf))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum RenderError {
|
|
|
|
Build(warp::http::Error),
|
2020-07-27 03:12:01 +00:00
|
|
|
IO(io::Error),
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl warp::reject::Reject for RenderError {}
|
|
|
|
|
|
|
|
pub async fn atom(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
|
|
|
HIT_COUNTER.with_label_values(&["atom"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
let mut buf = Vec::new();
|
2020-07-27 03:12:01 +00:00
|
|
|
templates::blog_atom_xml(&mut buf, state.everything.clone())
|
|
|
|
.map_err(RenderError::IO)
|
2020-07-16 19:32:30 +00:00
|
|
|
.map_err(warp::reject::custom)?;
|
|
|
|
Response::builder()
|
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/atom+xml")
|
|
|
|
.body(buf)
|
|
|
|
.map_err(RenderError::Build)
|
|
|
|
.map_err(warp::reject::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn rss(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
|
|
|
HIT_COUNTER.with_label_values(&["rss"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
let mut buf = Vec::new();
|
2020-07-27 03:12:01 +00:00
|
|
|
templates::blog_rss_xml(&mut buf, state.everything.clone())
|
|
|
|
.map_err(RenderError::IO)
|
2020-07-16 19:32:30 +00:00
|
|
|
.map_err(warp::reject::custom)?;
|
|
|
|
Response::builder()
|
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/rss+xml")
|
|
|
|
.body(buf)
|
|
|
|
.map_err(RenderError::Build)
|
|
|
|
.map_err(warp::reject::custom)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn sitemap(state: Arc<State>) -> Result<impl Reply, Rejection> {
|
|
|
|
HIT_COUNTER.with_label_values(&["sitemap"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
Response::builder()
|
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(state.sitemap.clone())
|
|
|
|
.map_err(RenderError::Build)
|
|
|
|
.map_err(warp::reject::custom)
|
|
|
|
}
|