1
0
Fork 0
website/src/handlers/posts.rs

30 lines
944 B
Rust
Raw Normal View History

2022-09-05 00:02:58 +02:00
use std::sync::Arc;
2022-09-05 00:05:24 +02:00
use axum::{extract::Path, response::Html, Extension};
2022-09-05 00:02:58 +02:00
use tracing::log::*;
2022-09-05 00:05:24 +02:00
use super::{Error, Result};
2023-03-25 12:23:11 +01:00
use crate::{post::render_post, State};
2022-09-05 00:02:58 +02:00
2023-03-22 22:39:18 +01:00
pub async fn view(Path(path): Path<String>, Extension(state): Extension<Arc<State>>) -> Result {
2023-03-25 12:23:11 +01:00
info!("Requested post: {}", path);
let post = state
.posts
.iter()
.find(|p| p.slug.eq_ignore_ascii_case(&path))
.ok_or(Error::NotFound)?;
//let post = load_post(&path).await.ok_or(Error::NotFound)?;
2023-03-22 22:39:18 +01:00
let res = render_post(&state, post).await.ok_or(Error::NotFound)?;
Ok(Html(res.into()))
}
2022-09-05 00:02:58 +02:00
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result {
let mut ctx = tera::Context::new();
ctx.insert("posts", &state.posts);
let res = state.tera.render("postsindex.html", &ctx).map_err(|e| {
2023-03-22 22:39:18 +01:00
error!("Failed rendering posts index: {:?}", e);
2022-09-05 00:02:58 +02:00
Error::NotFound
})?;
Ok(Html(res.into()))
}