aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/gallery.rs
blob: 25c8dfa67a557f488078ad6088836eb998e22c36 (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
use crate::{app::State, post::Post, tmpl};
use axum::extract::{Extension, Path};
use http::StatusCode;
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!("gallery_hits", "Number of hits to gallery images"),
        &["name"]
    )
    .unwrap();
}

#[instrument(skip(state))]
pub async fn index(Extension(state): Extension<Arc<State>>) -> Markup {
    let state = state.clone();
    tmpl::gallery_index(&state.gallery)
}

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

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

    match want {
        None => (StatusCode::NOT_FOUND, tmpl::not_found(link)),
        Some(post) => {
            HIT_COUNTER
                .with_label_values(&[name.clone().as_str()])
                .inc();
            (StatusCode::OK, tmpl::blog::gallery(&post))
        }
    }
}