2023-03-25 22:12:49 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-03-29 18:03:54 +02:00
|
|
|
use axum::{extract::{Path, State}, response::Html, routing::get, Extension, Router};
|
2023-03-25 22:12:49 +01:00
|
|
|
use serde_derive::Serialize;
|
|
|
|
use tracing::{instrument, log::*};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
post::{render_post, Post},
|
2023-03-29 18:03:54 +02:00
|
|
|
AppState, WebsiteError,
|
2023-03-25 22:12:49 +01:00
|
|
|
};
|
|
|
|
|
2023-03-29 18:03:54 +02:00
|
|
|
pub fn router() -> Router<Arc<AppState>> {
|
2023-03-25 22:12:49 +01:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2023-03-26 13:44:26 +02:00
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
struct PageContext<'a> {
|
2023-03-25 22:12:49 +01:00
|
|
|
title: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(skip(state))]
|
2023-03-29 18:03:54 +02:00
|
|
|
pub async fn index(State(state): State<Arc<AppState>>) -> Result<Html<String>, WebsiteError> {
|
2023-03-26 13:44:26 +02:00
|
|
|
let mut posts: Vec<&Post> = state
|
2023-03-26 12:01:59 +02:00
|
|
|
.posts
|
|
|
|
.values()
|
|
|
|
.filter(|p| p.is_published())
|
2023-03-26 13:44:26 +02:00
|
|
|
.collect();
|
2023-03-25 22:12:49 +01:00
|
|
|
|
|
|
|
posts.sort_by_key(|p| &p.date);
|
|
|
|
posts.reverse();
|
|
|
|
|
2023-03-26 13:44:26 +02:00
|
|
|
let ctx = PageContext {
|
2023-03-25 22:12:49 +01:00
|
|
|
title: "Posts",
|
|
|
|
};
|
|
|
|
|
2023-03-26 13:44:26 +02:00
|
|
|
let mut c = tera::Context::new();
|
|
|
|
c.insert("page", &ctx);
|
|
|
|
c.insert("posts", &posts);
|
|
|
|
|
|
|
|
let res = state.tera
|
|
|
|
.render("posts_index.html", &c)?;
|
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>,
|
2023-03-29 18:03:54 +02:00
|
|
|
State(state): State<Arc<AppState>>,
|
2023-03-26 12:40:25 +02:00
|
|
|
) -> 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?;
|
|
|
|
|
|
|
|
Ok(Html(res))
|
|
|
|
}
|
2023-03-26 13:05:39 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use chrono::DateTime;
|
|
|
|
|
|
|
|
use crate::post::Post;
|
|
|
|
|
2023-03-26 13:44:26 +02:00
|
|
|
use super::PageContext;
|
2023-03-26 13:05:39 +02:00
|
|
|
|
|
|
|
#[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()
|
|
|
|
}];
|
2023-03-26 13:44:26 +02:00
|
|
|
let page = PageContext {
|
2023-03-26 13:05:39 +02:00
|
|
|
title: "Posts",
|
|
|
|
};
|
2023-03-26 13:44:26 +02:00
|
|
|
let mut ctx = tera::Context::new();
|
|
|
|
ctx.insert("page", &page);
|
|
|
|
ctx.insert("posts", &posts);
|
|
|
|
|
2023-03-26 13:05:39 +02:00
|
|
|
let tera = tera::Tera::new("templates/**/*").unwrap();
|
|
|
|
let _res = tera
|
|
|
|
.render(
|
|
|
|
"posts_index.html",
|
2023-03-26 13:44:26 +02:00
|
|
|
&ctx,
|
2023-03-26 13:05:39 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|