diff options
| author | Xe Iaso <me@christine.website> | 2022-07-04 15:12:23 +0000 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2022-07-04 15:12:23 +0000 |
| commit | 8b6056fc09320473577f458fa86bda26159ea43b (patch) | |
| tree | 50b503cddf70719724c9e687791f827f002d0bd0 /src/handlers | |
| parent | 5d7daf179ee80f9f2be5345164a419fab051a651 (diff) | |
| download | xesite-8b6056fc09320473577f458fa86bda26159ea43b.tar.xz xesite-8b6056fc09320473577f458fa86bda26159ea43b.zip | |
add API calls for my blogposts/talks
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'src/handlers')
| -rw-r--r-- | src/handlers/blog.rs | 34 | ||||
| -rw-r--r-- | src/handlers/talks.rs | 34 |
2 files changed, 66 insertions, 2 deletions
diff --git a/src/handlers/blog.rs b/src/handlers/blog.rs index 6c66c97..cd935be 100644 --- a/src/handlers/blog.rs +++ b/src/handlers/blog.rs @@ -3,6 +3,7 @@ use crate::{app::State, post::Post, templates}; use axum::{ extract::{Extension, Path}, response::Html, + Json, }; use http::HeaderMap; use lazy_static::lazy_static; @@ -16,6 +17,11 @@ lazy_static! { &["name"] ) .unwrap(); + static ref HIT_COUNTER_JSON: IntCounterVec = register_int_counter_vec!( + opts!("blogpost_json_hits", "Number of hits to blogposts"), + &["name"] + ) + .unwrap(); } #[instrument(skip(state))] @@ -80,9 +86,10 @@ pub async fn post_view( headers: HeaderMap, ) -> Result { let mut want: Option<Post> = None; + let want_link = format!("blog/{}", name); for post in &state.blog { - if post.link == format!("blog/{}", name) { + if post.link == want_link { want = Some(post.clone()); } } @@ -107,3 +114,28 @@ pub async fn post_view( } } } + +#[instrument(skip(state))] +pub async fn post_json( + Path(name): Path<String>, + Extension(state): Extension<Arc<State>>, +) -> Result<Json<xe_jsonfeed::Item>> { + 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.clone()); + } + } + + match want { + None => Err(super::Error::PostNotFound(name)), + Some(post) => { + HIT_COUNTER_JSON + .with_label_values(&[name.clone().as_str()]) + .inc(); + Ok(Json(post.into())) + } + } +} diff --git a/src/handlers/talks.rs b/src/handlers/talks.rs index 3b33431..6d0e782 100644 --- a/src/handlers/talks.rs +++ b/src/handlers/talks.rs @@ -3,6 +3,7 @@ use crate::{app::State, post::Post, templates}; use axum::{ extract::{Extension, Path}, response::Html, + Json, }; use http::header::HeaderMap; use lazy_static::lazy_static; @@ -16,6 +17,11 @@ lazy_static! { &["name"] ) .unwrap(); + static ref HIT_COUNTER_JSON: IntCounterVec = register_int_counter_vec!( + opts!("talks_json_hits", "Number of hits to talks images"), + &["name"] + ) + .unwrap(); } #[instrument(skip(state))] @@ -33,9 +39,10 @@ pub async fn post_view( headers: HeaderMap, ) -> Result { let mut want: Option<Post> = None; + let want_link = format!("talks/{}", name); for post in &state.talks { - if post.link == format!("talks/{}", name) { + if post.link == want_link { want = Some(post.clone()); } } @@ -60,3 +67,28 @@ pub async fn post_view( } } } + +#[instrument(skip(state))] +pub async fn post_json( + Path(name): Path<String>, + Extension(state): Extension<Arc<State>>, +) -> Result<Json<xe_jsonfeed::Item>> { + let mut want: Option<Post> = None; + let want_link = format!("talks/{}", name); + + for post in &state.talks { + if post.link == want_link { + want = Some(post.clone()); + } + } + + match want { + None => Err(super::Error::PostNotFound(name)), + Some(post) => { + HIT_COUNTER_JSON + .with_label_values(&[name.clone().as_str()]) + .inc(); + Ok(Json(post.into())) + } + } +} |
