2022-03-22 00:14:14 +00:00
|
|
|
use super::{Error::*, Result};
|
|
|
|
use crate::{app::State, post::Post, templates};
|
|
|
|
use axum::{
|
|
|
|
extract::{Extension, Path},
|
|
|
|
response::Html,
|
2020-07-16 19:32:30 +00:00
|
|
|
};
|
|
|
|
use lazy_static::lazy_static;
|
2021-03-13 14:03:19 +00:00
|
|
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
2020-07-16 19:32:30 +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
|
|
|
static ref HIT_COUNTER: IntCounterVec = register_int_counter_vec!(
|
|
|
|
opts!("talks_hits", "Number of hits to talks images"),
|
|
|
|
&["name"]
|
|
|
|
)
|
|
|
|
.unwrap();
|
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 index(Extension(state): Extension<Arc<State>>) -> Result {
|
2020-07-16 19:32:30 +00:00
|
|
|
let state = state.clone();
|
2022-03-22 00:14:14 +00:00
|
|
|
let mut result: Vec<u8> = vec![];
|
|
|
|
templates::talkindex_html(&mut result, state.talks.clone())?;
|
|
|
|
Ok(Html(result))
|
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 post_view(
|
|
|
|
Path(name): Path<String>,
|
|
|
|
Extension(state): Extension<Arc<State>>,
|
|
|
|
) -> Result {
|
2020-07-16 19:32:30 +00:00
|
|
|
let mut want: Option<Post> = None;
|
|
|
|
|
|
|
|
for post in &state.talks {
|
|
|
|
if post.link == format!("talks/{}", name) {
|
|
|
|
want = Some(post.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match want {
|
2022-03-22 00:14:14 +00:00
|
|
|
None => Err(PostNotFound(name).into()),
|
2020-07-16 19:32:30 +00:00
|
|
|
Some(post) => {
|
2021-03-13 14:03:19 +00:00
|
|
|
HIT_COUNTER
|
|
|
|
.with_label_values(&[name.clone().as_str()])
|
|
|
|
.inc();
|
2022-03-22 00:14:14 +00:00
|
|
|
let body = templates::Html(post.body_html.clone());
|
|
|
|
let mut result: Vec<u8> = vec![];
|
|
|
|
templates::talkpost_html(&mut result, post, body)?;
|
|
|
|
Ok(Html(result))
|
2020-07-16 19:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|