diff options
Diffstat (limited to 'src/handlers/feeds.rs')
| -rw-r--r-- | src/handlers/feeds.rs | 95 |
1 files changed, 26 insertions, 69 deletions
diff --git a/src/handlers/feeds.rs b/src/handlers/feeds.rs index 3d540f6..c69e2f0 100644 --- a/src/handlers/feeds.rs +++ b/src/handlers/feeds.rs @@ -1,10 +1,14 @@ -use super::LAST_MODIFIED; -use crate::{app::State, post::Post, templates}; +use super::{Result, LAST_MODIFIED}; +use crate::{ + app::State, + post::{NewPost, Post}, + templates, +}; +use axum::{body, extract::Extension, response::Response, Json}; use lazy_static::lazy_static; use prometheus::{opts, register_int_counter_vec, IntCounterVec}; -use std::{io, sync::Arc}; +use std::sync::Arc; use tracing::instrument; -use warp::{http::Response, Rejection, Reply}; lazy_static! { pub static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!( @@ -16,103 +20,56 @@ lazy_static! { } #[instrument(skip(state))] -pub async fn jsonfeed(state: Arc<State>, since: Option<String>) -> Result<impl Reply, Rejection> { +pub async fn jsonfeed(Extension(state): Extension<Arc<State>>) -> Json<jsonfeed::Feed> { HIT_COUNTER.with_label_values(&["json"]).inc(); let state = state.clone(); - Ok(warp::reply::json(&state.jf)) + Json(state.jf.clone()) } #[instrument(skip(state))] -pub async fn new_post(state: Arc<State>) -> Result<impl Reply, Rejection> { +#[axum_macros::debug_handler] +pub async fn new_post(Extension(state): Extension<Arc<State>>) -> Result<Json<NewPost>> { let state = state.clone(); - let everything = state.everything.clone(); - let p: &Post = everything.iter().next().unwrap(); - Ok(warp::reply::json(&p.new_post)) + let p: Post = state.everything.iter().next().unwrap().clone(); + Ok(Json(p.new_post)) } -#[derive(Debug)] -pub enum RenderError { - Build(warp::http::Error), - IO(io::Error), -} - -impl warp::reject::Reject for RenderError {} - #[instrument(skip(state))] -pub async fn atom(state: Arc<State>, since: Option<String>) -> Result<impl Reply, Rejection> { - if let Some(etag) = since { - if etag == ETAG.clone() { - return Response::builder() - .status(304) - .header("Content-Type", "text/plain") - .body( - "You already have the newest version of this feed." - .to_string() - .into_bytes(), - ) - .map_err(RenderError::Build) - .map_err(warp::reject::custom); - } - } - +pub async fn atom(Extension(state): Extension<Arc<State>>) -> Result<Response> { HIT_COUNTER.with_label_values(&["atom"]).inc(); let state = state.clone(); let mut buf = Vec::new(); - templates::blog_atom_xml(&mut buf, state.everything.clone()) - .map_err(RenderError::IO) - .map_err(warp::reject::custom)?; - Response::builder() + templates::blog_atom_xml(&mut buf, state.everything.clone())?; + Ok(Response::builder() .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) + .body(body::boxed(body::Full::from(buf)))?) } #[instrument(skip(state))] -pub async fn rss(state: Arc<State>, since: Option<String>) -> Result<impl Reply, Rejection> { - if let Some(etag) = since { - if etag == ETAG.clone() { - return Response::builder() - .status(304) - .header("Content-Type", "text/plain") - .body( - "You already have the newest version of this feed." - .to_string() - .into_bytes(), - ) - .map_err(RenderError::Build) - .map_err(warp::reject::custom); - } - } - +pub async fn rss(Extension(state): Extension<Arc<State>>) -> Result<Response> { HIT_COUNTER.with_label_values(&["rss"]).inc(); let state = state.clone(); let mut buf = Vec::new(); - templates::blog_rss_xml(&mut buf, state.everything.clone()) - .map_err(RenderError::IO) - .map_err(warp::reject::custom)?; - Response::builder() + templates::blog_rss_xml(&mut buf, state.everything.clone())?; + Ok(Response::builder() .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) + .body(body::boxed(body::Full::from(buf)))?) } #[instrument(skip(state))] -pub async fn sitemap(state: Arc<State>) -> Result<impl Reply, Rejection> { +#[axum_macros::debug_handler] +pub async fn sitemap(Extension(state): Extension<Arc<State>>) -> Result<Response> { HIT_COUNTER.with_label_values(&["sitemap"]).inc(); let state = state.clone(); - Response::builder() + Ok(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) + .body(body::boxed(body::Full::from(state.sitemap.clone())))?) } |
