1
0
Fork 0
This commit is contained in:
Adrian Hedqvist 2023-03-26 13:44:26 +02:00
parent d585ba3f92
commit 5625ff418b
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"))
}
#[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<Arc<State>>) -> Result<Html<String>, WebsiteError> {
let mut posts = state
let mut posts: Vec<&Post> = state
.posts
.values()
.filter(|p| p.is_published())
.collect::<Vec<&Post>>();
.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();
}

View file

@ -1,15 +1,22 @@
{% extends "base.html" %}
{% 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>
<h2>todo</h2>
<ul>
<li>static content ✅</li>
<li>sass compilation</li>
<li>post metadata (frontmatter) ✅</li>
<li>rss/atom/jsonfeed</li>
<li>proper error handling ✅ (i guess??)</li>
<li>other pages???</li>
<li>✅ static content</li>
<li>✅ template rendering (tera)</li>
<li>✅ markdown rendering (pulldown_cmark)</li>
<li>✅ post metadata (frontmatter, toml)</li>
<li>✅ app metrics</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>
{% endblock main %}