small fixes and things

This commit is contained in:
slonkazoid 2024-12-01 22:01:14 +03:00
parent 9fb372574d
commit ff2eae0ae1
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM
11 changed files with 53 additions and 66 deletions

View file

@ -3,6 +3,7 @@ name = "bingus-blog"
version = "0.1.0"
edition = "2021"
default-run = "bingus-blog"
repository = "https://git.slonk.ing/slonk/bingus-blog"
[features]
default = []

View file

@ -5,7 +5,7 @@ use std::time::Duration;
use axum::extract::{Path, Query, State};
use axum::http::header::CONTENT_TYPE;
use axum::http::Request;
use axum::response::{IntoResponse, Redirect, Response};
use axum::response::{Html, IntoResponse, Redirect, Response};
use axum::routing::get;
use axum::{Json, Router};
use handlebars::Handlebars;
@ -26,6 +26,19 @@ use crate::serve_dir_included::handle;
const STATIC: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/static");
#[derive(Serialize)]
pub struct BingusInfo {
pub name: &'static str,
pub version: &'static str,
pub repository: &'static str,
}
const BINGUS_INFO: BingusInfo = BingusInfo {
name: env!("CARGO_PKG_NAME"),
version: env!("CARGO_PKG_VERSION"),
repository: env!("CARGO_PKG_REPOSITORY"),
};
#[derive(Clone)]
#[non_exhaustive]
pub struct AppState {
@ -36,6 +49,7 @@ pub struct AppState {
#[derive(Serialize)]
struct IndexTemplate<'a> {
bingus_info: &'a BingusInfo,
title: &'a str,
description: &'a str,
posts: Vec<PostMetadata>,
@ -48,6 +62,7 @@ struct IndexTemplate<'a> {
#[derive(Serialize)]
struct PostTemplate<'a> {
bingus_info: &'a BingusInfo,
meta: &'a PostMetadata,
rendered: String,
rendered_in: RenderStats,
@ -124,6 +139,7 @@ async fn index<'a>(
&IndexTemplate {
title: &config.title,
description: &config.description,
bingus_info: &BINGUS_INFO,
posts,
rss: config.rss.enable,
js: config.js_enable,
@ -133,7 +149,7 @@ async fn index<'a>(
},
);
drop(reg);
Ok(([(CONTENT_TYPE, "text/html")], rendered?))
Ok(Html(rendered?))
}
async fn all_posts(
@ -220,6 +236,7 @@ async fn post(
let rendered = reg.render(
"post",
&PostTemplate {
bingus_info: &BINGUS_INFO,
meta,
rendered,
rendered_in,
@ -234,7 +251,7 @@ async fn post(
},
);
drop(reg);
Ok(([(CONTENT_TYPE, "text/html")], rendered?).into_response())
Ok(Html(rendered?).into_response())
}
ReturnedPost::Raw(body, content_type) => {
Ok(([(CONTENT_TYPE, content_type)], body).into_response())

View file

@ -143,7 +143,6 @@ impl Default for DisplayDates {
}
}
impl Default for DirsConfig {
fn default() -> Self {
Self {
@ -194,7 +193,7 @@ pub async fn load() -> Result<Config> {
"{}_CONFIG",
env!("CARGO_BIN_NAME").to_uppercase().replace('-', "_")
))
.unwrap_or(String::from("config.toml"));
.unwrap_or_else(|_| String::from("config.toml"));
match tokio::fs::OpenOptions::new()
.read(true)
.open(&config_file)

View file

@ -203,13 +203,7 @@ where
if stat.is_file() && path.extension().is_some_and(|ext| ext == "md") {
let mtime = as_secs(&stat.modified()?);
// TODO. this?
let name = path
.clone()
.file_stem()
.unwrap()
.to_string_lossy()
.to_string();
let name = String::from(path.file_stem().unwrap().to_string_lossy());
if let Some(cache) = self.cache.as_ref()
&& let Some(hit) = cache.lookup_metadata(&name, mtime).await

View file

@ -8,7 +8,6 @@ use thiserror::Error;
use tracing::{debug, error, info_span, trace};
const TEMPLATES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/templates");
const PARTIALS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/partials");
#[derive(Error, Debug)]
#[allow(clippy::enum_variant_names)]
@ -29,7 +28,7 @@ fn is_ext(path: impl AsRef<Path>, ext: &str) -> bool {
}
}
fn get_template_name(path: &Path) -> Option<&str> {
fn get_template_name(path: &Path) -> Option<&str> {
if !is_ext(path, "hbs") {
return None;
}
@ -57,15 +56,6 @@ fn register_path(
Ok(())
}
fn register_partial(
file: &include_dir::File<'_>,
name: &str,
registry: &mut Handlebars,
) -> Result<(), TemplateError> {
registry.register_partial(name, file.contents_utf8().ok_or(TemplateError::UTF8Error)?)?;
Ok(())
}
fn compile_included_file(file: &include_dir::File<'_>) -> Result<Template, TemplateError> {
let contents = file.contents_utf8().ok_or(TemplateError::UTF8Error)?;
@ -85,7 +75,7 @@ fn compile_path(path: impl AsRef<std::path::Path>) -> Result<Template, TemplateE
Ok(template)
}
async fn compile_path_async_io(
async fn compile_path_async_io(
path: impl AsRef<std::path::Path>,
) -> Result<Template, TemplateError> {
use tokio::fs::OpenOptions;
@ -125,29 +115,6 @@ pub fn new_registry<'a>(custom_templates_path: impl AsRef<Path>) -> io::Result<H
};
}
for entry in PARTIALS.entries() {
let file = match entry.as_file() {
Some(file) => file,
None => continue,
};
let span = info_span!("register_partial", path = ?file.path());
let _handle = span.enter();
let name = match get_template_name(file.path()) {
Some(v) => v,
None => {
trace!("skipping file");
continue;
}
};
match register_partial(file, name, &mut reg) {
Ok(()) => debug!("registered partial {name:?}"),
Err(err) => error!("error while registering partial: {err}"),
};
}
let read_dir = match std::fs::read_dir(custom_templates_path) {
Ok(v) => v,
Err(err) => match err.kind() {

21
templates/footer.hbs Normal file
View file

@ -0,0 +1,21 @@
running <a href="{{bingus_info.repository}}" target="_blank">{{bingus_info.name}}</a> v{{bingus_info.version}}
{{#if rendered_in}}
<b> - </b>
{{/if}}
{{#each rendered_in}}
{{#if (eq @key "ParsedAndRendered")}}
<span class="tooltipped" title="parsing took {{duration this.1}}">parsed</span>
and
<span class="tooltipped" title="rendering took {{duration this.2}}">rendered</span>
in
{{duration this.0}}
{{else if (eq @key "Cached")}}
retrieved from cache in
{{duration this}}
{{/if}}
{{/each}}
{{#if markdown_access}}
-
<a href="/posts/{{meta.name}}.md">view raw</a>
{{/if}}

View file

@ -22,8 +22,7 @@
</head>
<body>
<main>
<h1>{{title}}</h1>
<p>{{description}}</p>
{{>title}}
<h2>posts</h2>
<div>
{{#if js}}
@ -59,5 +58,8 @@
<span class="post-author">- {{this}} post{{#if (ne this 1)}}s{{/if}}</span><br />
{{/each}}
</main>
<footer>
{{>footer}}
</footer>
</body>
</html>

View file

@ -49,23 +49,7 @@
{{{rendered}}}
</main>
<footer>
{{#each rendered_in}}
{{#if (eq @key "ParsedAndRendered")}}
<span class="tooltipped" title="parsing took {{duration this.[1]}}">parsed</span>
and
<span class="tooltipped" title="rendering took {{duration this.[2]}}">rendered</span>
in
{{duration this.[0]}}
{{else if (eq @key "Cached")}}
retrieved from cache in
{{duration this}}
{{/if}}
{{/each}}
{{#if markdown_access}}
-
<a href="/posts/{{meta.name}}.md">view raw</a>
{{/if}}
{{>footer}}
</footer>
</body>
</html>

2
templates/title.hbs Normal file
View file

@ -0,0 +1,2 @@
<h1>{{title}}</h1>
<p>{{description}}</p>