2022-03-22 00:14:14 +00:00
|
|
|
use super::{Result, LAST_MODIFIED};
|
|
|
|
use crate::{
|
|
|
|
app::State,
|
|
|
|
post::{NewPost, Post},
|
|
|
|
templates,
|
|
|
|
};
|
|
|
|
use axum::{body, extract::Extension, response::Response, Json};
|
2020-07-16 19:32:30 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
2022-03-22 00:14:14 +00:00
|
|
|
use std::sync::Arc;
|
2020-10-02 22:36:57 +00:00
|
|
|
use tracing::instrument;
|
2020-07-16 19:32:30 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2021-03-13 14:03:19 +00:00
|
|
|
pub static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
|
2020-07-16 19:32:30 +00:00
|
|
|
opts!("feed_hits", "Number of hits to various feeds"),
|
|
|
|
&["kind"]
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-01-15 03:36:34 +00:00
|
|
|
pub static ref ETAG: String = format!(r#"W/"{}""#, uuid::Uuid::new_v4().to_simple());
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:36:57 +00:00
|
|
|
#[instrument(skip(state))]
|
2022-07-04 14:44:00 +00:00
|
|
|
pub async fn jsonfeed(Extension(state): Extension<Arc<State>>) -> Json<xe_jsonfeed::Feed> {
|
2020-07-16 19:32:30 +00:00
|
|
|
HIT_COUNTER.with_label_values(&["json"]).inc();
|
|
|
|
let state = state.clone();
|
2022-03-22 00:14:14 +00:00
|
|
|
Json(state.jf.clone())
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 00:12:42 +00:00
|
|
|
#[instrument(skip(state))]
|
2022-03-22 00:14:14 +00:00
|
|
|
#[axum_macros::debug_handler]
|
|
|
|
pub async fn new_post(Extension(state): Extension<Arc<State>>) -> Result<Json<NewPost>> {
|
2021-07-06 00:12:42 +00:00
|
|
|
let state = state.clone();
|
2022-03-22 00:14:14 +00:00
|
|
|
let p: Post = state.everything.iter().next().unwrap().clone();
|
|
|
|
Ok(Json(p.new_post))
|
2021-07-06 00:12:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:36:57 +00:00
|
|
|
#[instrument(skip(state))]
|
2022-03-22 00:14:14 +00:00
|
|
|
pub async fn atom(Extension(state): Extension<Arc<State>>) -> Result<Response> {
|
2020-07-16 19:32:30 +00:00
|
|
|
HIT_COUNTER.with_label_values(&["atom"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
let mut buf = Vec::new();
|
2022-03-22 00:14:14 +00:00
|
|
|
templates::blog_atom_xml(&mut buf, state.everything.clone())?;
|
|
|
|
Ok(Response::builder()
|
2020-07-16 19:32:30 +00:00
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/atom+xml")
|
2021-01-15 03:36:34 +00:00
|
|
|
.header("ETag", ETAG.clone())
|
2021-03-13 14:03:19 +00:00
|
|
|
.header("Last-Modified", &*LAST_MODIFIED)
|
2022-03-22 00:14:14 +00:00
|
|
|
.body(body::boxed(body::Full::from(buf)))?)
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:36:57 +00:00
|
|
|
#[instrument(skip(state))]
|
2022-03-22 00:14:14 +00:00
|
|
|
pub async fn rss(Extension(state): Extension<Arc<State>>) -> Result<Response> {
|
2020-07-16 19:32:30 +00:00
|
|
|
HIT_COUNTER.with_label_values(&["rss"]).inc();
|
|
|
|
let state = state.clone();
|
|
|
|
let mut buf = Vec::new();
|
2022-03-22 00:14:14 +00:00
|
|
|
templates::blog_rss_xml(&mut buf, state.everything.clone())?;
|
|
|
|
Ok(Response::builder()
|
2020-07-16 19:32:30 +00:00
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/rss+xml")
|
2021-01-15 03:36:34 +00:00
|
|
|
.header("ETag", ETAG.clone())
|
2021-03-13 14:03:19 +00:00
|
|
|
.header("Last-Modified", &*LAST_MODIFIED)
|
2022-03-22 00:14:14 +00:00
|
|
|
.body(body::boxed(body::Full::from(buf)))?)
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 22:36:57 +00:00
|
|
|
#[instrument(skip(state))]
|
2022-03-22 00:14:14 +00:00
|
|
|
#[axum_macros::debug_handler]
|
|
|
|
pub async fn sitemap(Extension(state): Extension<Arc<State>>) -> Result<Response> {
|
2020-07-16 19:32:30 +00:00
|
|
|
HIT_COUNTER.with_label_values(&["sitemap"]).inc();
|
|
|
|
let state = state.clone();
|
2022-03-22 00:14:14 +00:00
|
|
|
Ok(Response::builder()
|
2020-07-16 19:32:30 +00:00
|
|
|
.status(200)
|
|
|
|
.header("Content-Type", "application/xml")
|
2021-03-13 14:03:19 +00:00
|
|
|
.header("Last-Modified", &*LAST_MODIFIED)
|
2022-03-22 00:14:14 +00:00
|
|
|
.body(body::boxed(body::Full::from(state.sitemap.clone())))?)
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|