forked from cadey/xesite
add API calls for my blogposts/talks
Signed-off-by: Xe Iaso <me@christine.website>
This commit is contained in:
parent
5d7daf179e
commit
8b6056fc09
|
@ -3,6 +3,7 @@ use crate::{app::State, post::Post, templates};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Path},
|
extract::{Extension, Path},
|
||||||
response::Html,
|
response::Html,
|
||||||
|
Json,
|
||||||
};
|
};
|
||||||
use http::HeaderMap;
|
use http::HeaderMap;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
@ -16,6 +17,11 @@ lazy_static! {
|
||||||
&["name"]
|
&["name"]
|
||||||
)
|
)
|
||||||
.unwrap();
|
.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))]
|
#[instrument(skip(state))]
|
||||||
|
@ -80,9 +86,10 @@ pub async fn post_view(
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let mut want: Option<Post> = None;
|
let mut want: Option<Post> = None;
|
||||||
|
let want_link = format!("blog/{}", name);
|
||||||
|
|
||||||
for post in &state.blog {
|
for post in &state.blog {
|
||||||
if post.link == format!("blog/{}", name) {
|
if post.link == want_link {
|
||||||
want = Some(post.clone());
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ use crate::{app::State, post::Post, templates};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Path},
|
extract::{Extension, Path},
|
||||||
response::Html,
|
response::Html,
|
||||||
|
Json,
|
||||||
};
|
};
|
||||||
use http::header::HeaderMap;
|
use http::header::HeaderMap;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
@ -16,6 +17,11 @@ lazy_static! {
|
||||||
&["name"]
|
&["name"]
|
||||||
)
|
)
|
||||||
.unwrap();
|
.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))]
|
#[instrument(skip(state))]
|
||||||
|
@ -33,9 +39,10 @@ pub async fn post_view(
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let mut want: Option<Post> = None;
|
let mut want: Option<Post> = None;
|
||||||
|
let want_link = format!("talks/{}", name);
|
||||||
|
|
||||||
for post in &state.talks {
|
for post in &state.talks {
|
||||||
if post.link == format!("talks/{}", name) {
|
if post.link == want_link {
|
||||||
want = Some(post.clone());
|
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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -160,6 +160,8 @@ async fn main() -> Result<()> {
|
||||||
"/api/salary_transparency.json",
|
"/api/salary_transparency.json",
|
||||||
get(handlers::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
|
// static pages
|
||||||
.route("/", get(handlers::index))
|
.route("/", get(handlers::index))
|
||||||
.route("/contact", get(handlers::contact))
|
.route("/contact", get(handlers::contact))
|
||||||
|
|
Loading…
Reference in New Issue