2023-03-25 22:12:49 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use axum::{extract::Path, response::Html, routing::get, Extension, Router};
|
|
|
|
use serde_derive::Serialize;
|
|
|
|
use tracing::{instrument, log::*};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
post::{render_post, Post},
|
|
|
|
State, WebsiteError,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::HIT_COUNTER;
|
|
|
|
|
|
|
|
pub fn router() -> Router {
|
|
|
|
Router::new()
|
|
|
|
.route("/", get(index))
|
|
|
|
.route("/:slug/", get(view))
|
2023-03-26 12:40:25 +02:00
|
|
|
.route("/:slug/index.md", get(super::not_found))
|
2023-03-25 22:12:49 +01:00
|
|
|
.fallback_service(tower_http::services::ServeDir::new("./posts"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct IndexContext<'a> {
|
|
|
|
title: &'a str,
|
|
|
|
posts: Vec<&'a Post>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(state))]
|
|
|
|
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result<Html<String>, WebsiteError> {
|
2023-03-26 12:01:59 +02:00
|
|
|
let mut posts = state
|
|
|
|
.posts
|
|
|
|
.values()
|
|
|
|
.filter(|p| p.is_published())
|
|
|
|
.collect::<Vec<&Post>>();
|
2023-03-25 22:12:49 +01:00
|
|
|
|
|
|
|
posts.sort_by_key(|p| &p.date);
|
|
|
|
posts.reverse();
|
|
|
|
|
|
|
|
let ctx = IndexContext {
|
|
|
|
title: "Posts",
|
|
|
|
posts,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = match state
|
|
|
|
.tera
|
|
|
|
.render("posts_index.html", &tera::Context::from_serialize(ctx)?)
|
|
|
|
{
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
error!("failed to render posts index: {}", e);
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-26 00:30:21 +01:00
|
|
|
HIT_COUNTER.with_label_values(&["/posts/"]).inc();
|
2023-03-25 22:12:49 +01:00
|
|
|
Ok(Html(res))
|
|
|
|
}
|
2023-03-26 12:40:25 +02:00
|
|
|
|
|
|
|
#[instrument(skip(state))]
|
|
|
|
pub async fn view(
|
|
|
|
Path(slug): Path<String>,
|
|
|
|
Extension(state): Extension<Arc<State>>,
|
|
|
|
) -> Result<Html<String>, WebsiteError> {
|
|
|
|
let post = state.posts.get(&slug).ok_or(WebsiteError::NotFound)?;
|
|
|
|
if !post.is_published() {
|
|
|
|
warn!("attempted to view post before it has been published!");
|
|
|
|
return Err(WebsiteError::NotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = render_post(&state.tera, post).await?;
|
|
|
|
|
|
|
|
HIT_COUNTER
|
|
|
|
.with_label_values(&[&format!("/posts/{slug}/")])
|
|
|
|
.inc();
|
|
|
|
Ok(Html(res))
|
|
|
|
}
|
2023-03-26 13:05:39 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use chrono::DateTime;
|
|
|
|
|
|
|
|
use crate::post::Post;
|
|
|
|
|
|
|
|
use super::IndexContext;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn render_index() {
|
|
|
|
let posts = vec![Post {
|
|
|
|
title: "test".into(),
|
|
|
|
slug: "test".into(),
|
|
|
|
date: Some(DateTime::parse_from_rfc3339("2023-03-26T13:04:01+02:00").unwrap()),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
Post {
|
|
|
|
title: "test2".into(),
|
|
|
|
slug: "test2".into(),
|
|
|
|
date: None,
|
|
|
|
..Default::default()
|
|
|
|
}];
|
|
|
|
let ctx = IndexContext {
|
|
|
|
title: "Posts",
|
|
|
|
posts: posts.iter().collect(),
|
|
|
|
};
|
|
|
|
let tera = tera::Tera::new("templates/**/*").unwrap();
|
|
|
|
let _res = tera
|
|
|
|
.render(
|
|
|
|
"posts_index.html",
|
|
|
|
&tera::Context::from_serialize(ctx).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|