diff --git a/README.md b/README.md index bd91866..a9f6405 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,8 @@ standard. examples of valid and invalid dates: ## Non-static Routes - `GET /`: index page, lists posts -- `GET /posts`: returns a list of all posts with metadata in JSON format +- `GET /posts`: small preview of posts for embedding in other sites and such +- `GET /posts.json`: returns a list of all posts with metadata in JSON format - `GET /posts/`: view a post - `GET /posts/.md`: view the raw markdown of a post - `GET /post/*`: redirects to `/posts/*` diff --git a/src/app.rs b/src/app.rs index 1f943bd..e6bb338 100644 --- a/src/app.rs +++ b/src/app.rs @@ -60,6 +60,14 @@ struct IndexTemplate<'a> { style: &'a StyleConfig, } +#[derive(Serialize)] +struct PostsTemplate<'a> { + bingus_info: &'a BingusInfo, + posts: Vec, + js: bool, + style: &'a StyleConfig, +} + #[derive(Serialize)] struct PostTemplate<'a> { bingus_info: &'a BingusInfo, @@ -119,7 +127,7 @@ async fn index( rss, style, posts, - templates: reg, + templates, .. }): State, Query(query): Query, @@ -135,7 +143,7 @@ async fn index( let tags = collect_tags(&posts); let joined_tags = join_tags_for_meta(&tags, ", "); - let reg = reg.read().await; + let reg = templates.read().await; let style = style.load(); let rendered = reg.render( "index", @@ -154,7 +162,7 @@ async fn index( Ok(Html(rendered?)) } -async fn all_posts( +async fn posts_json( State(AppState { posts, .. }): State, Query(query): Query, ) -> AppResult>> { @@ -169,6 +177,39 @@ async fn all_posts( Ok(Json(posts)) } +async fn posts( + State(AppState { + posts, + templates, + style, + .. + }): State, + Query(query): Query, +) -> AppResult> { + let posts = posts + .get_max_n_post_metadata_with_optional_tag_sorted( + query.num_posts, + query.tag.as_deref(), + &query.other, + ) + .await?; + + let reg = templates.read().await; + let style = style.load(); + let rendered = reg.render( + "index", + &PostsTemplate { + bingus_info: &BINGUS_INFO, + posts, + js: style.js_enable, + style: &style, + }, + ); + drop((style, reg)); + + Ok(Html(rendered?)) +} + async fn rss( State(AppState { rss, style, posts, .. @@ -237,7 +278,7 @@ async fn post( State(AppState { style, posts, - templates: reg, + templates, .. }): State, Path(name): Path>, @@ -252,7 +293,7 @@ async fn post( } => { let joined_tags = meta.tags.join(", "); - let reg = reg.read().await; + let reg = templates.read().await; let style = style.load(); let rendered = reg.render( "post", @@ -289,7 +330,8 @@ pub fn new(dirs: &DirsConfig) -> Router { ), ) .route("/posts/:name", get(post)) - .route("/posts", get(all_posts)) + .route("/posts", get(posts)) + .route("/posts.json", get(posts_json)) .route("/feed.xml", get(rss)) .nest_service( "/static", diff --git a/templates/posts.hbs b/templates/posts.hbs index d6dab3d..3453982 100644 --- a/templates/posts.hbs +++ b/templates/posts.hbs @@ -5,7 +5,7 @@ {{title}} {{#if rss}} - + {{/if}} {{#if js}}