aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2022-07-04 15:12:23 +0000
committerXe Iaso <me@christine.website>2022-07-04 15:12:23 +0000
commit8b6056fc09320473577f458fa86bda26159ea43b (patch)
tree50b503cddf70719724c9e687791f827f002d0bd0
parent5d7daf179ee80f9f2be5345164a419fab051a651 (diff)
downloadxesite-8b6056fc09320473577f458fa86bda26159ea43b.tar.xz
xesite-8b6056fc09320473577f458fa86bda26159ea43b.zip
add API calls for my blogposts/talks
Signed-off-by: Xe Iaso <me@christine.website>
-rw-r--r--src/handlers/blog.rs34
-rw-r--r--src/handlers/talks.rs34
-rw-r--r--src/main.rs2
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<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()))
+ }
+ }
+}
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))