1
0
Fork 0

fix posts index page, minor tweaks

This commit is contained in:
Adrian Hedqvist 2024-04-21 14:27:29 +02:00
parent 93f734bdf0
commit 81993fe829
3 changed files with 13 additions and 27 deletions

View file

@ -18,6 +18,7 @@ anyway here's a new todo list:
- [x] docker from-scratch image (it's small!) - [x] docker from-scratch image (it's small!)
- [x] opentelemetry (metrics, traces) - [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?) - [ ] 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) - [ ] 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) - [ ] ~~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)) - [ ] fancy css (but nothing too fancy, I like it [Simple And Clean](https://youtu.be/0nKizH5TV_g?t=42))

View file

@ -13,7 +13,7 @@ use axum::{
use serde_derive::Serialize; use serde_derive::Serialize;
use tower::ServiceExt; use tower::ServiceExt;
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tracing::{debug, instrument}; use tracing::instrument;
use crate::{ use crate::{
page::{render_page, Page}, page::{render_page, Page},
@ -61,6 +61,7 @@ async fn index(
let mut c = tera::Context::new(); let mut c = tera::Context::new();
c.insert("page", &ctx); c.insert("page", &ctx);
c.insert("posts", &posts); c.insert("posts", &posts);
c.insert("site_title", &state.settings.title);
c.insert("base_url", &state.base_url.to_string()); c.insert("base_url", &state.base_url.to_string());
let res = state.tera.render("section_index.html", &c)?; let res = state.tera.render("section_index.html", &c)?;
@ -95,7 +96,7 @@ async fn view(
return Ok(Redirect::permanent(p).into_response()); 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('/') { if !uri.path().ends_with('/') {
let p = format!("{}/", uri.path()); let p = format!("{}/", uri.path());
if state.pages.contains_key(&p) { if state.pages.contains_key(&p) {
@ -103,9 +104,7 @@ async fn view(
} }
} }
debug!("page not found for '{uri}', fallback to static files"); // no page, check if there's a static file to serve from here
// 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
return get_static_file("pages", uri).await; return get_static_file("pages", uri).await;
}; };
@ -115,10 +114,8 @@ async fn view(
// Check if they have the current page cached // Check if they have the current page cached
if let Some(etag) = headers.get(header::IF_NONE_MATCH) { if let Some(etag) = headers.get(header::IF_NONE_MATCH) {
if let Ok(etag) = etag.to_str() { if etag.to_str().ok() == Some(post.etag.as_str()) {
if etag == post.etag { return Ok(StatusCode::NOT_MODIFIED.into_response());
return Ok(StatusCode::NOT_MODIFIED.into_response());
}
} }
} }
@ -167,34 +164,20 @@ pub async fn feed(
posts.reverse(); posts.reverse();
posts.truncate(10); posts.truncate(10);
let last_modified =
last_changed.map_or_else(|| state.startup_time.to_rfc2822(), |d| d.to_rfc2822());
Ok(( Ok((
StatusCode::OK, StatusCode::OK,
[ [
(header::CONTENT_TYPE, "application/atom+xml"), (header::CONTENT_TYPE, "application/atom+xml"),
( (header::LAST_MODIFIED, &last_modified),
header::LAST_MODIFIED,
&last_changed.map_or_else(|| state.startup_time.to_rfc2822(), |d| d.to_rfc2822()),
),
], ],
crate::feed::render_atom_feed(&state)?, crate::feed::render_atom_feed(&state)?,
) )
.into_response()) .into_response())
} }
#[instrument(skip(state))]
pub async fn redirect(
uri: OriginalUri,
State(state): State<Arc<AppState>>,
) -> Result<Redirect, WebsiteError> {
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<Response, WebsiteError> { async fn get_static_file(base_dir: &str, uri: Uri) -> Result<Response, WebsiteError> {
let req = Request::builder().uri(uri).body(Body::empty()).unwrap(); let req = Request::builder().uri(uri).body(Body::empty()).unwrap();

View file

@ -4,12 +4,14 @@
<p>I occasionally write some stuff, it's quite rare but it does happen believe it or not.</p> <p>I occasionally write some stuff, it's quite rare but it does happen believe it or not.</p>
<ul> <ul>
{% for post in posts -%} {% for post in posts -%}
{% if post.title -%}
<li><a href="{{base_url | trim_end_matches(pat='/') | safe}}{{post.absolute_path | safe}}">{% if post.date -%} <li><a href="{{base_url | trim_end_matches(pat='/') | safe}}{{post.absolute_path | safe}}">{% if post.date -%}
<time datetime="{{ post.date }}">{{ post.date | date(format="%F") }}</time> &ndash; {{ post.title -}} <time datetime="{{ post.date }}">{{ post.date | date(format="%F") }}</time> &ndash; {{ post.title -}}
{% else -%} {% else -%}
{{ post.title -}} {{ post.title -}}
{% endif -%} {% endif -%}
</a></li> </a></li>
{%- endif %}
{% endfor -%} {% endfor -%}
</ul> </ul>
{% endblock main -%} {% endblock main -%}