add /posts and rename old one to /posts.json

This commit is contained in:
slonkazoid 2025-01-29 17:47:50 +03:00
parent d92bf1bbea
commit 9b482858f4
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
3 changed files with 51 additions and 8 deletions

View file

@ -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/<name>`: view a post
- `GET /posts/<name>.md`: view the raw markdown of a post
- `GET /post/*`: redirects to `/posts/*`

View file

@ -60,6 +60,14 @@ struct IndexTemplate<'a> {
style: &'a StyleConfig,
}
#[derive(Serialize)]
struct PostsTemplate<'a> {
bingus_info: &'a BingusInfo,
posts: Vec<PostMetadata>,
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<AppState>,
Query(query): Query<QueryParams>,
@ -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<AppState>,
Query(query): Query<QueryParams>,
) -> AppResult<Json<Vec<PostMetadata>>> {
@ -169,6 +177,39 @@ async fn all_posts(
Ok(Json(posts))
}
async fn posts(
State(AppState {
posts,
templates,
style,
..
}): State<AppState>,
Query(query): Query<QueryParams>,
) -> AppResult<Html<String>> {
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<AppState>,
Path(name): Path<Arc<str>>,
@ -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<AppState> {
),
)
.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",

View file

@ -5,7 +5,7 @@
<title>{{title}}</title>
<link rel="stylesheet" href="/static/style.css" />
{{#if rss}}
<link rel="alternate" type="application/rss+xml" title="{{title}}" href="/feed.xml" />
<link rel="alternate" type="application/rss+xml" title="{{style.title}}" href="/feed.xml" />
{{/if}}
{{#if js}}
<script src="/static/date.js" defer></script>