aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/blog.rs
blob: f4565423ea25e027b40606e0e1113bcb9eabfb65 (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
use super::Result;
use crate::{app::State, post::Post, tmpl};
use axum::{
    extract::{Extension, Path},
    http::StatusCode,
};
use http::HeaderMap;
use lazy_static::lazy_static;
use maud::Markup;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
use std::sync::Arc;
use tracing::instrument;

lazy_static! {
    static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
        opts!("blogpost_hits", "Number of hits to blogposts"),
        &["name"]
    )
    .unwrap();
}

#[instrument(skip(state))]
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result<Markup> {
    let state = state.clone();
    let result = tmpl::post_index(&state.blog, "Blogposts", true);
    Ok(result)
}

#[instrument(skip(state))]
pub async fn series(Extension(state): Extension<Arc<State>>) -> Result<Markup> {
    let state = state.clone();

    Ok(tmpl::blog_series(&state.cfg.clone().series_descriptions))
}

#[instrument(skip(state))]
pub async fn series_view(
    Path(series): Path<String>,
    Extension(state): Extension<Arc<State>>,
) -> (StatusCode, Markup) {
    let state = state.clone();
    let cfg = state.cfg.clone();
    let mut posts: Vec<Post> = vec![];

    for post in &state.blog {
        if post.front_matter.series.is_none() {
            continue;
        }
        if post.front_matter.series.as_ref().unwrap() != &series {
            continue;
        }
        posts.push(post.clone());
    }

    posts.reverse();

    let desc = cfg.series_desc_map.get(&series);

    if posts.is_empty() {
        (
            StatusCode::NOT_FOUND,
            tmpl::error(format!("series not found: {series}")),
        )
    } else if let Some(desc) = desc {
        (StatusCode::OK, tmpl::series_view(&series, desc, &posts))
    } else {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            tmpl::error(format!("series metadata in dhall not found: {series}")),
        )
    }
}

#[instrument(skip(state, headers))]
pub async fn post_view(
    Path(name): Path<String>,
    Extension(state): Extension<Arc<State>>,
    headers: HeaderMap,
) -> Result<(StatusCode, Markup)> {
    let mut want: Option<&Post> = None;
    let want_link = format!("blog/{}", name);

    for post in &state.blog {
        if post.link == want_link {
            want = Some(post);
        }
    }

    let referer = if let Some(referer) = headers.get(http::header::REFERER) {
        let referer = referer.to_str()?.to_string();
        Some(referer)
    } else {
        None
    };

    match want {
        None => Ok((StatusCode::NOT_FOUND, tmpl::not_found(want_link))),
        Some(post) => {
            HIT_COUNTER
                .with_label_values(&[name.clone().as_str()])
                .inc();
            let body = maud::PreEscaped(&post.body_html);
            Ok((StatusCode::OK, tmpl::blog::blog(post, body, referer)))
        }
    }
}