aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/feeds.rs
blob: d3f0289d6f076465ecc3785bd367c26fdcef0cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use super::LAST_MODIFIED;
use crate::{app::State, templates};
use lazy_static::lazy_static;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
use std::{io, sync::Arc};
use tracing::instrument;
use warp::{http::Response, Rejection, Reply};

lazy_static! {
    pub static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
        opts!("feed_hits", "Number of hits to various feeds"),
        &["kind"]
    )
    .unwrap();
    pub static ref ETAG: String = format!(r#"W/"{}""#, uuid::Uuid::new_v4().to_simple());
}

#[instrument(skip(state))]
pub async fn jsonfeed(state: Arc<State>, since: Option<String>) -> 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),
    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);
        }
    }

    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()
        .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)
}

#[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);
        }
    }

    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()
        .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)
}

#[instrument(skip(state))]
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")
        .header("Last-Modified", &*LAST_MODIFIED)
        .body(state.sitemap.clone())
        .map_err(RenderError::Build)
        .map_err(warp::reject::custom)
}