From 81993fe829edd2e5762f61a153f175f56264c88b Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Sun, 21 Apr 2024 14:27:29 +0200 Subject: [PATCH] fix posts index page, minor tweaks --- pages/index.md | 1 + src/handlers/pages.rs | 37 ++++++++++-------------------------- templates/section_index.html | 2 ++ 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/pages/index.md b/pages/index.md index 7bcf119..85485fe 100644 --- a/pages/index.md +++ b/pages/index.md @@ -18,6 +18,7 @@ anyway here's a new todo list: - [x] docker from-scratch image (it's small!) - [x] opentelemetry (metrics, traces) - [ ] opentelemetry logs? (don't know if I'm gonna need it? can probably just make the collector grab them from the docker logs?) +- [ ] sections (currently the posts page is hardcoded, should be able to turn any page-subfolder into its own section) - [ ] file-watching (rebuild pages when they're changed, not only on startup) - [ ] ~~sass/less compilation~~ (don't think I need it, will skip for now) - [ ] fancy css (but nothing too fancy, I like it [Simple And Clean](https://youtu.be/0nKizH5TV_g?t=42)) diff --git a/src/handlers/pages.rs b/src/handlers/pages.rs index 623604a..6551fe9 100644 --- a/src/handlers/pages.rs +++ b/src/handlers/pages.rs @@ -13,7 +13,7 @@ use axum::{ use serde_derive::Serialize; use tower::ServiceExt; use tower_http::services::ServeDir; -use tracing::{debug, instrument}; +use tracing::instrument; use crate::{ page::{render_page, Page}, @@ -61,6 +61,7 @@ async fn index( let mut c = tera::Context::new(); c.insert("page", &ctx); c.insert("posts", &posts); + c.insert("site_title", &state.settings.title); c.insert("base_url", &state.base_url.to_string()); let res = state.tera.render("section_index.html", &c)?; @@ -95,7 +96,7 @@ async fn view( return Ok(Redirect::permanent(p).into_response()); } - // No alias, check if there's an easy redirect + // No alias, check if we can do a simple redirect if !uri.path().ends_with('/') { let p = format!("{}/", uri.path()); if state.pages.contains_key(&p) { @@ -103,9 +104,7 @@ async fn view( } } - debug!("page not found for '{uri}', fallback to static files"); - - // TODO: I don't like how we create a new oneshot for every 404 request, but I don't know if there's a better way + // no page, check if there's a static file to serve from here return get_static_file("pages", uri).await; }; @@ -115,10 +114,8 @@ async fn view( // Check if they have the current page cached if let Some(etag) = headers.get(header::IF_NONE_MATCH) { - if let Ok(etag) = etag.to_str() { - if etag == post.etag { - return Ok(StatusCode::NOT_MODIFIED.into_response()); - } + if etag.to_str().ok() == Some(post.etag.as_str()) { + return Ok(StatusCode::NOT_MODIFIED.into_response()); } } @@ -167,34 +164,20 @@ pub async fn feed( posts.reverse(); posts.truncate(10); + let last_modified = + last_changed.map_or_else(|| state.startup_time.to_rfc2822(), |d| d.to_rfc2822()); + Ok(( StatusCode::OK, [ (header::CONTENT_TYPE, "application/atom+xml"), - ( - header::LAST_MODIFIED, - &last_changed.map_or_else(|| state.startup_time.to_rfc2822(), |d| d.to_rfc2822()), - ), + (header::LAST_MODIFIED, &last_modified), ], crate::feed::render_atom_feed(&state)?, ) .into_response()) } -#[instrument(skip(state))] -pub async fn redirect( - uri: OriginalUri, - State(state): State>, -) -> Result { - let path = uri.path(); - let p = format!("{path}/"); - if state.pages.contains_key(&p) { - Ok(Redirect::permanent(&format!("{path}/"))) - } else { - Err(WebsiteError::NotFound) - } -} - async fn get_static_file(base_dir: &str, uri: Uri) -> Result { let req = Request::builder().uri(uri).body(Body::empty()).unwrap(); diff --git a/templates/section_index.html b/templates/section_index.html index 1842e4b..07cf79f 100644 --- a/templates/section_index.html +++ b/templates/section_index.html @@ -4,12 +4,14 @@

I occasionally write some stuff, it's quite rare but it does happen believe it or not.

{% endblock main -%}