add /posts and rename old one to /posts.json
This commit is contained in:
parent
d92bf1bbea
commit
9b482858f4
3 changed files with 51 additions and 8 deletions
|
@ -110,7 +110,8 @@ standard. examples of valid and invalid dates:
|
||||||
## Non-static Routes
|
## Non-static Routes
|
||||||
|
|
||||||
- `GET /`: index page, lists posts
|
- `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>`: view a post
|
||||||
- `GET /posts/<name>.md`: view the raw markdown of a post
|
- `GET /posts/<name>.md`: view the raw markdown of a post
|
||||||
- `GET /post/*`: redirects to `/posts/*`
|
- `GET /post/*`: redirects to `/posts/*`
|
||||||
|
|
54
src/app.rs
54
src/app.rs
|
@ -60,6 +60,14 @@ struct IndexTemplate<'a> {
|
||||||
style: &'a StyleConfig,
|
style: &'a StyleConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct PostsTemplate<'a> {
|
||||||
|
bingus_info: &'a BingusInfo,
|
||||||
|
posts: Vec<PostMetadata>,
|
||||||
|
js: bool,
|
||||||
|
style: &'a StyleConfig,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct PostTemplate<'a> {
|
struct PostTemplate<'a> {
|
||||||
bingus_info: &'a BingusInfo,
|
bingus_info: &'a BingusInfo,
|
||||||
|
@ -119,7 +127,7 @@ async fn index(
|
||||||
rss,
|
rss,
|
||||||
style,
|
style,
|
||||||
posts,
|
posts,
|
||||||
templates: reg,
|
templates,
|
||||||
..
|
..
|
||||||
}): State<AppState>,
|
}): State<AppState>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
|
@ -135,7 +143,7 @@ async fn index(
|
||||||
let tags = collect_tags(&posts);
|
let tags = collect_tags(&posts);
|
||||||
let joined_tags = join_tags_for_meta(&tags, ", ");
|
let joined_tags = join_tags_for_meta(&tags, ", ");
|
||||||
|
|
||||||
let reg = reg.read().await;
|
let reg = templates.read().await;
|
||||||
let style = style.load();
|
let style = style.load();
|
||||||
let rendered = reg.render(
|
let rendered = reg.render(
|
||||||
"index",
|
"index",
|
||||||
|
@ -154,7 +162,7 @@ async fn index(
|
||||||
Ok(Html(rendered?))
|
Ok(Html(rendered?))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn all_posts(
|
async fn posts_json(
|
||||||
State(AppState { posts, .. }): State<AppState>,
|
State(AppState { posts, .. }): State<AppState>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> AppResult<Json<Vec<PostMetadata>>> {
|
) -> AppResult<Json<Vec<PostMetadata>>> {
|
||||||
|
@ -169,6 +177,39 @@ async fn all_posts(
|
||||||
Ok(Json(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(
|
async fn rss(
|
||||||
State(AppState {
|
State(AppState {
|
||||||
rss, style, posts, ..
|
rss, style, posts, ..
|
||||||
|
@ -237,7 +278,7 @@ async fn post(
|
||||||
State(AppState {
|
State(AppState {
|
||||||
style,
|
style,
|
||||||
posts,
|
posts,
|
||||||
templates: reg,
|
templates,
|
||||||
..
|
..
|
||||||
}): State<AppState>,
|
}): State<AppState>,
|
||||||
Path(name): Path<Arc<str>>,
|
Path(name): Path<Arc<str>>,
|
||||||
|
@ -252,7 +293,7 @@ async fn post(
|
||||||
} => {
|
} => {
|
||||||
let joined_tags = meta.tags.join(", ");
|
let joined_tags = meta.tags.join(", ");
|
||||||
|
|
||||||
let reg = reg.read().await;
|
let reg = templates.read().await;
|
||||||
let style = style.load();
|
let style = style.load();
|
||||||
let rendered = reg.render(
|
let rendered = reg.render(
|
||||||
"post",
|
"post",
|
||||||
|
@ -289,7 +330,8 @@ pub fn new(dirs: &DirsConfig) -> Router<AppState> {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.route("/posts/:name", get(post))
|
.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))
|
.route("/feed.xml", get(rss))
|
||||||
.nest_service(
|
.nest_service(
|
||||||
"/static",
|
"/static",
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<title>{{title}}</title>
|
<title>{{title}}</title>
|
||||||
<link rel="stylesheet" href="/static/style.css" />
|
<link rel="stylesheet" href="/static/style.css" />
|
||||||
{{#if rss}}
|
{{#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}}
|
||||||
{{#if js}}
|
{{#if js}}
|
||||||
<script src="/static/date.js" defer></script>
|
<script src="/static/date.js" defer></script>
|
||||||
|
|
Loading…
Reference in a new issue