From 58f199c35f2fc4833c74903a0553562db0299a13 Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Sun, 26 Mar 2023 13:44:26 +0200 Subject: [PATCH] things --- README.md | 11 +++++++++++ src/handlers/posts.rs | 39 ++++++++++++++++++--------------------- templates/index.html | 21 ++++++++++++++------- 3 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..78af854 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# + +## 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 diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index c3b7de8..d476d8d 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -19,38 +19,32 @@ pub fn router() -> Router { .fallback_service(tower_http::services::ServeDir::new("./posts")) } -#[derive(Serialize)] -struct IndexContext<'a> { +#[derive(Serialize, Debug)] +struct PageContext<'a> { title: &'a str, - posts: Vec<&'a Post>, } #[instrument(skip(state))] pub async fn index(Extension(state): Extension>) -> Result, WebsiteError> { - let mut posts = state + let mut posts: Vec<&Post> = state .posts .values() .filter(|p| p.is_published()) - .collect::>(); + .collect(); posts.sort_by_key(|p| &p.date); posts.reverse(); - let ctx = IndexContext { + let ctx = PageContext { title: "Posts", - posts, }; - let res = match state - .tera - .render("posts_index.html", &tera::Context::from_serialize(ctx)?) - { - Ok(r) => r, - Err(e) => { - error!("failed to render posts index: {}", e); - return Err(e.into()); - } - }; + let mut c = tera::Context::new(); + c.insert("page", &ctx); + c.insert("posts", &posts); + + let res = state.tera + .render("posts_index.html", &c)?; HIT_COUNTER.with_label_values(&["/posts/"]).inc(); Ok(Html(res)) @@ -81,7 +75,7 @@ mod tests { use crate::post::Post; - use super::IndexContext; + use super::PageContext; #[test] fn render_index() { @@ -97,15 +91,18 @@ mod tests { date: None, ..Default::default() }]; - let ctx = IndexContext { + let page = PageContext { 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 _res = tera .render( "posts_index.html", - &tera::Context::from_serialize(ctx).unwrap(), + &ctx, ) .unwrap(); } diff --git a/templates/index.html b/templates/index.html index f63c2b8..6c76c31 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,15 +1,22 @@ {% extends "base.html" %} {% block main %} -

tollyx

+

tollyx.net

hi hello welcome to my website it's pretty wip right now yeah ok bye

todo

    -
  • static content ✅
  • -
  • sass compilation
  • -
  • post metadata (frontmatter) ✅
  • -
  • rss/atom/jsonfeed
  • -
  • proper error handling ✅ (i guess??)
  • -
  • other pages???
  • +
  • ✅ static content
  • +
  • ✅ template rendering (tera)
  • +
  • ✅ markdown rendering (pulldown_cmark)
  • +
  • ✅ post metadata (frontmatter, toml)
  • +
  • ✅ app metrics
  • +
  • ✅ tests
  • +
  • ⬜ page aliases (redirects, for back-compat with old routes)
  • +
  • ⬜ sass compilation (rsass? grass?)
  • +
  • ⬜ rss/atom/jsonfeed
  • +
  • ✅ proper error handling (i guess??)
  • +
  • ⬜ other pages???
  • +
  • ⬜ opentelemetry?
  • +
  • ⬜ fancy styling
{% endblock main %} \ No newline at end of file