1
Fork 0
mirror of https://github.com/RGBCube/Site synced 2025-08-01 13:37:49 +00:00

Add blog stuff

This commit is contained in:
RGBCube 2024-01-08 13:25:01 +03:00
parent 1c85d7e720
commit 9a292fce5b
No known key found for this signature in database
3 changed files with 74 additions and 22 deletions

61
src/routes/blog.rs Normal file
View file

@ -0,0 +1,61 @@
use axum::extract::Path;
use maud::{
html,
Markup,
};
use super::markdown::PAGES;
use crate::{
errors::not_found,
page::{
text,
Page,
},
};
pub async fn index_handler() -> Markup {
text::create(
Some("blog"),
Page::Blog,
&html! {
h1 { "Blog Articles" }
p { "RSS feed coming soon, probably :)" }
ul {
@let pages = &*PAGES;
@for (path, (metadata, ..)) in pages.iter() {
@if path.starts_with("blog") {
li {
(metadata.date.unwrap().format("%d/%m/%Y"))
" - "
a href=(format!("/{path}")) {
(metadata.title)
}
}
}
}
}
},
)
}
pub async fn entry_handler(Path(path): Path<String>) -> Markup {
if let Some((metadata, body)) = PAGES.get(&path) {
text::create(
Some(&metadata.title),
Page::Other,
&html! {
(body)
@if let Some(tags) = &metadata.tags {
p {
"Tags: "
(tags.join(", "))
}
}
},
)
} else {
not_found::handler().await
}
}

View file

@ -4,15 +4,7 @@ use std::{
sync::LazyLock, sync::LazyLock,
}; };
use axum::{ use axum::extract::Path;
body::Body,
extract::Path,
http::Response,
response::{
Html,
IntoResponse,
},
};
use chrono::NaiveDate; use chrono::NaiveDate;
use maud::Markup; use maud::Markup;
use serde::Deserialize; use serde::Deserialize;
@ -26,17 +18,14 @@ use crate::{
}, },
}; };
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
struct Metadata { pub struct Metadata {
title: String, pub title: String,
// TODO: Use these for blog articles. pub date: Option<NaiveDate>,
#[allow(dead_code)] pub tags: Option<Vec<String>>,
date: Option<NaiveDate>,
#[allow(dead_code)]
tags: Option<Vec<String>>,
} }
static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| { pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| {
let routes_path = path::Path::new(file!()) let routes_path = path::Path::new(file!())
.parent() .parent()
.unwrap() .unwrap()
@ -72,11 +61,10 @@ static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| {
pages pages
}); });
pub async fn handler(Path(path): Path<String>) -> Response<Body> { pub async fn handler(Path(path): Path<String>) -> Markup {
if let Some((metadata, body)) = PAGES.get(&path) { if let Some((metadata, body)) = PAGES.get(&path) {
Html(text::create(Some(&metadata.title), Page::from_str(&path), &body).into_string()) text::create(Some(&metadata.title), Page::from_str(&path), &body)
.into_response()
} else { } else {
not_found::handler().await.into_response() not_found::handler().await
} }
} }

View file

@ -4,12 +4,15 @@ use axum::{
}; };
mod assets; mod assets;
mod blog;
mod index; mod index;
mod markdown; mod markdown;
pub fn router() -> Router { pub fn router() -> Router {
Router::new() Router::new()
.route("/", get(index::handler)) .route("/", get(index::handler))
.route("/blog", get(blog::index_handler))
.route("/blog/:entry", get(blog::entry_handler))
.route("/*page", get(markdown::handler)) .route("/*page", get(markdown::handler))
.route("/assets/*path", get(assets::handler)) .route("/assets/*path", get(assets::handler))
} }