From 8b6056fc09320473577f458fa86bda26159ea43b Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Mon, 4 Jul 2022 15:12:23 +0000 Subject: [PATCH] add API calls for my blogposts/talks Signed-off-by: Xe Iaso --- src/handlers/blog.rs | 34 +++++++++++++++++++++++++++++++++- src/handlers/talks.rs | 34 +++++++++++++++++++++++++++++++++- src/main.rs | 2 ++ 3 files changed, 68 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 = 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, + Extension(state): Extension>, +) -> Result> { + let mut want: Option = 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 = 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, + Extension(state): Extension>, +) -> Result> { + let mut want: Option = 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())) + } + } +} diff --git a/src/main.rs b/src/main.rs index eb4b9e5..e820ba5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,6 +160,8 @@ async fn main() -> Result<()> { "/api/salary_transparency.json", get(handlers::salary_transparency_json), ) + .route("/api/blog/:name", get(handlers::blog::post_json)) + .route("/api/talks/:name", get(handlers::talks::post_json)) // static pages .route("/", get(handlers::index)) .route("/contact", get(handlers::contact))