1
0
Fork 0
This commit is contained in:
Adrian Hedqvist 2023-03-26 13:44:26 +02:00
parent d585ba3f92
commit 58f199c35f
3 changed files with 43 additions and 28 deletions

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# <https://tollyx.net>
## My personal website written in rust
Inspired by [fasterthanli.me], [xeiaso.net], and my own curiousity for learning
how to do web-dev stuff in rust, I started working on this thing.
I do not recommend using this for yourself, at most take a peek how I've done
things and use it as an inspiration on how to write your own website in rust.
[fasterthanli.me]: https://fasterthanli.me
[xeiaso.net]: https://xeiaso.net

View file

@ -19,38 +19,32 @@ pub fn router() -> Router {
.fallback_service(tower_http::services::ServeDir::new("./posts")) .fallback_service(tower_http::services::ServeDir::new("./posts"))
} }
#[derive(Serialize)] #[derive(Serialize, Debug)]
struct IndexContext<'a> { struct PageContext<'a> {
title: &'a str, title: &'a str,
posts: Vec<&'a Post>,
} }
#[instrument(skip(state))] #[instrument(skip(state))]
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result<Html<String>, WebsiteError> { pub async fn index(Extension(state): Extension<Arc<State>>) -> Result<Html<String>, WebsiteError> {
let mut posts = state let mut posts: Vec<&Post> = state
.posts .posts
.values() .values()
.filter(|p| p.is_published()) .filter(|p| p.is_published())
.collect::<Vec<&Post>>(); .collect();
posts.sort_by_key(|p| &p.date); posts.sort_by_key(|p| &p.date);
posts.reverse(); posts.reverse();
let ctx = IndexContext { let ctx = PageContext {
title: "Posts", title: "Posts",
posts,
}; };
let res = match state let mut c = tera::Context::new();
.tera c.insert("page", &ctx);
.render("posts_index.html", &tera::Context::from_serialize(ctx)?) c.insert("posts", &posts);
{
Ok(r) => r, let res = state.tera
Err(e) => { .render("posts_index.html", &c)?;
error!("failed to render posts index: {}", e);
return Err(e.into());
}
};
HIT_COUNTER.with_label_values(&["/posts/"]).inc(); HIT_COUNTER.with_label_values(&["/posts/"]).inc();
Ok(Html(res)) Ok(Html(res))
@ -81,7 +75,7 @@ mod tests {
use crate::post::Post; use crate::post::Post;
use super::IndexContext; use super::PageContext;
#[test] #[test]
fn render_index() { fn render_index() {
@ -97,15 +91,18 @@ mod tests {
date: None, date: None,
..Default::default() ..Default::default()
}]; }];
let ctx = IndexContext { let page = PageContext {
title: "Posts", title: "Posts",
posts: posts.iter().collect(),
}; };
let mut ctx = tera::Context::new();
ctx.insert("page", &page);
ctx.insert("posts", &posts);
let tera = tera::Tera::new("templates/**/*").unwrap(); let tera = tera::Tera::new("templates/**/*").unwrap();
let _res = tera let _res = tera
.render( .render(
"posts_index.html", "posts_index.html",
&tera::Context::from_serialize(ctx).unwrap(), &ctx,
) )
.unwrap(); .unwrap();
} }

View file

@ -1,15 +1,22 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block main %} {% block main %}
<h1>tollyx</h1> <h1>tollyx.net</h1>
<p>hi hello welcome to my website it's pretty wip right now yeah ok bye</p> <p>hi hello welcome to my website it's pretty wip right now yeah ok bye</p>
<h2>todo</h2> <h2>todo</h2>
<ul> <ul>
<li>static content ✅</li> <li>✅ static content</li>
<li>sass compilation</li> <li>✅ template rendering (tera)</li>
<li>post metadata (frontmatter) ✅</li> <li>✅ markdown rendering (pulldown_cmark)</li>
<li>rss/atom/jsonfeed</li> <li>✅ post metadata (frontmatter, toml)</li>
<li>proper error handling ✅ (i guess??)</li> <li>✅ app metrics</li>
<li>other pages???</li> <li>✅ tests</li>
<li>⬜ page aliases (redirects, for back-compat with old routes)</li>
<li>⬜ sass compilation (rsass? grass?)</li>
<li>⬜ rss/atom/jsonfeed</li>
<li>✅ proper error handling (i guess??)</li>
<li>⬜ other pages???</li>
<li>⬜ opentelemetry?</li>
<li>⬜ fancy styling</li>
</ul> </ul>
{% endblock main %} {% endblock main %}