Files
crawlnicle/src/handlers/entry.rs
Tyler Hallada 340cbc4971 Working apalis cron and worker with 0.6.0-rc.5
Also renamed `pool` variables throughout codebase to `db` for clarity.
2024-08-21 01:10:26 -04:00

49 lines
1.6 KiB
Rust

use std::fs;
use axum::extract::{Path, State};
use axum::response::Response;
use axum_extra::TypedHeader;
use maud::{html, PreEscaped};
use sqlx::PgPool;
use crate::config::Config;
use crate::error::Result;
use crate::htmx::HXTarget;
use crate::models::entry::Entry;
use crate::partials::layout::Layout;
use crate::partials::time::date_time;
use crate::uuid::Base62Uuid;
pub async fn get(
Path(id): Path<Base62Uuid>,
State(db): State<PgPool>,
State(config): State<Config>,
hx_target: Option<TypedHeader<HXTarget>>,
layout: Layout,
) -> Result<Response> {
let entry = Entry::get(&db, id.as_uuid()).await?;
let content_dir = std::path::Path::new(&config.content_dir);
let content_path = content_dir.join(format!("{}.html", entry.entry_id));
let title = entry.title.unwrap_or_else(|| "Untitled Entry".to_string());
let content = fs::read_to_string(content_path).unwrap_or_else(|_| "No content".to_string());
Ok(layout
.with_subtitle(&title)
.targeted(hx_target)
.render(html! {
article class="prose lg:prose-xl my-6 mx-auto prose-a:text-blue-600 prose-a:no-underline visited:prose-a:text-purple-600 hover:prose-a:underline" {
header {
h2 class="mb-4 text-2xl font-medium" {
a href=(entry.url) { (title) }
}
}
div {
span class="text-sm text-gray-600" {
strong { "Published: " }
(date_time(entry.published_at))
}
}
(PreEscaped(content))
}
}))
}