aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/api.rs
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2022-07-10 09:58:57 -0400
committerXe Iaso <me@christine.website>2022-07-10 09:58:57 -0400
commitb32f5a25afb7b9901476164663c1b7099dcec7a8 (patch)
tree3c97a73d002c46fef0184fad6c8c7fcf9b7bc30a /src/handlers/api.rs
parent18ae8a01f883f19870df86bfa2e5cf288f79bce4 (diff)
downloadxesite-b32f5a25afb7b9901476164663c1b7099dcec7a8.tar.xz
xesite-b32f5a25afb7b9901476164663c1b7099dcec7a8.zip
consolidate API routes
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'src/handlers/api.rs')
-rw-r--r--src/handlers/api.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/handlers/api.rs b/src/handlers/api.rs
new file mode 100644
index 0000000..835d282
--- /dev/null
+++ b/src/handlers/api.rs
@@ -0,0 +1,78 @@
+use crate::{
+ app::{config::Job, State},
+ handlers::Result,
+ post::Post,
+};
+use axum::extract::{Extension, Json, Path};
+use lazy_static::lazy_static;
+use prometheus::{opts, register_int_counter_vec, IntCounterVec};
+use std::sync::Arc;
+
+lazy_static! {
+ static ref BLOG: IntCounterVec = register_int_counter_vec!(
+ opts!("blogpost_json_hits", "Number of hits to blogposts"),
+ &["name"]
+ )
+ .unwrap();
+ static ref TALK: IntCounterVec = register_int_counter_vec!(
+ opts!("talks_json_hits", "Number of hits to talks images"),
+ &["name"]
+ )
+ .unwrap();
+}
+
+#[axum_macros::debug_handler]
+#[instrument(skip(state))]
+pub async fn salary_transparency(Extension(state): Extension<Arc<State>>) -> Json<Vec<Job>> {
+ super::HIT_COUNTER
+ .with_label_values(&["salary_transparency_json"])
+ .inc();
+
+ Json(state.clone().cfg.clone().job_history.clone())
+}
+
+#[instrument(skip(state))]
+pub async fn blog(
+ 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) => {
+ BLOG.with_label_values(&[name.clone().as_str()]).inc();
+ Ok(Json(post.into()))
+ }
+ }
+}
+
+#[instrument(skip(state))]
+pub async fn talk(
+ 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) => {
+ TALK.with_label_values(&[name.clone().as_str()]).inc();
+ Ok(Json(post.into()))
+ }
+ }
+}