1
0
Fork 0
website/src/handlers/posts.rs
2023-03-25 12:24:34 +01:00

29 lines
944 B
Rust

use std::sync::Arc;
use axum::{extract::Path, response::Html, Extension};
use tracing::log::*;
use super::{Error, Result};
use crate::{post::render_post, State};
pub async fn view(Path(path): Path<String>, Extension(state): Extension<Arc<State>>) -> Result {
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)?;
let res = render_post(&state, post).await.ok_or(Error::NotFound)?;
Ok(Html(res.into()))
}
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| {
error!("Failed rendering posts index: {:?}", e);
Error::NotFound
})?;
Ok(Html(res.into()))
}