Refactor
Some checks failed
CI / Check links (push) Failing after 31s
CI / Check typos (push) Successful in 52s
CI / Clippy (push) Successful in 1m11s
CI / Build and test (push) Failing after 1m12s
CI / Build container (push) Has been skipped
CI / Deploy on waypoint (push) Has been skipped
Some checks failed
CI / Check links (push) Failing after 31s
CI / Check typos (push) Successful in 52s
CI / Clippy (push) Successful in 1m11s
CI / Build and test (push) Failing after 1m12s
CI / Build container (push) Has been skipped
CI / Deploy on waypoint (push) Has been skipped
This commit is contained in:
@@ -12,6 +12,7 @@ libservice = { workspace = true }
|
||||
macro-assets = { workspace = true }
|
||||
macro-sass = { workspace = true }
|
||||
assetserver = { workspace = true }
|
||||
page = { workspace = true }
|
||||
|
||||
axum = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
@@ -25,6 +26,5 @@ lazy_static = { workspace = true }
|
||||
serde_yaml = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
lru = { workspace = true }
|
||||
tower-http = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
|
||||
@@ -3,11 +3,13 @@ use markdown_it::parser::block::{BlockRule, BlockState};
|
||||
use markdown_it::parser::core::Root;
|
||||
use markdown_it::parser::inline::{InlineRule, InlineState};
|
||||
use markdown_it::{MarkdownIt, Node, NodeValue, Renderer};
|
||||
use maud::{Markup, PreEscaped, Render};
|
||||
use maud::{Markup, PreEscaped, Render, html};
|
||||
use page::{Page, PageMetadata};
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::components::fa::FAIcon;
|
||||
use crate::components::mangle::{MangledBetaEmail, MangledGoogleEmail};
|
||||
use crate::components::misc::Backlinks;
|
||||
|
||||
lazy_static! {
|
||||
static ref MdParser: MarkdownIt = {
|
||||
@@ -38,6 +40,58 @@ impl Markdown<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: helpers
|
||||
//
|
||||
|
||||
/// Try to read page metadata from a markdown file's frontmatter.
|
||||
/// - returns `none` if there is no frontmatter
|
||||
/// - returns an error if we fail to parse frontmatter
|
||||
pub fn meta_from_markdown(root_node: &Node) -> Result<Option<PageMetadata>, serde_yaml::Error> {
|
||||
root_node
|
||||
.children
|
||||
.first()
|
||||
.and_then(|x| x.cast::<FrontMatter>())
|
||||
.map(|x| serde_yaml::from_str::<PageMetadata>(&x.content))
|
||||
.map_or(Ok(None), |v| v.map(Some))
|
||||
}
|
||||
|
||||
pub fn page_from_markdown(md: impl Into<String>, default_image: Option<String>) -> Page {
|
||||
let md: String = md.into();
|
||||
let md = Markdown::parse(&md);
|
||||
|
||||
let mut meta = meta_from_markdown(&md)
|
||||
.unwrap_or(Some(PageMetadata {
|
||||
title: "Invalid frontmatter!".into(),
|
||||
..Default::default()
|
||||
}))
|
||||
.unwrap_or_default();
|
||||
|
||||
if meta.image.is_none() {
|
||||
meta.image = default_image
|
||||
}
|
||||
|
||||
let html = PreEscaped(md.render());
|
||||
|
||||
Page {
|
||||
meta,
|
||||
generate_html: Box::new(move |page, _| {
|
||||
let html = html.clone();
|
||||
Box::pin(async move {
|
||||
html! {
|
||||
@if let Some(slug) = &page.meta.slug {
|
||||
(Backlinks(&[("/", "home")], slug))
|
||||
}
|
||||
|
||||
(html)
|
||||
}
|
||||
})
|
||||
}),
|
||||
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: extensions
|
||||
//
|
||||
|
||||
@@ -2,7 +2,6 @@ use axum::Router;
|
||||
use libservice::ToService;
|
||||
|
||||
mod components;
|
||||
mod page;
|
||||
mod pages;
|
||||
mod routes;
|
||||
|
||||
|
||||
@@ -1,428 +0,0 @@
|
||||
//
|
||||
// MARK: metadata
|
||||
//
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
extract::{ConnectInfo, Path, State},
|
||||
http::{HeaderMap, HeaderValue, StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use libservice::ServiceConnectInfo;
|
||||
use lru::LruCache;
|
||||
use markdown_it::Node;
|
||||
use maud::{Markup, PreEscaped, Render, html};
|
||||
use parking_lot::Mutex;
|
||||
use serde::Deserialize;
|
||||
use std::{collections::HashMap, num::NonZero, pin::Pin, sync::Arc, time::Instant};
|
||||
use tower_http::compression::{CompressionLayer, DefaultPredicate};
|
||||
use tracing::{trace, warn};
|
||||
|
||||
use crate::components::{
|
||||
md::{FrontMatter, Markdown},
|
||||
misc::Backlinks,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
|
||||
pub struct PageMetadata {
|
||||
pub title: String,
|
||||
pub author: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub image: Option<String>,
|
||||
pub slug: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for PageMetadata {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: "Untitled page".into(),
|
||||
author: None,
|
||||
description: None,
|
||||
image: None,
|
||||
slug: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for PageMetadata {
|
||||
fn render(&self) -> Markup {
|
||||
let empty = String::new();
|
||||
let title = &self.title;
|
||||
let author = &self.author.as_ref().unwrap_or(&empty);
|
||||
let description = &self.description.as_ref().unwrap_or(&empty);
|
||||
let image = &self.image.as_ref().unwrap_or(&empty);
|
||||
|
||||
html !(
|
||||
meta property="og:site_name" content=(title) {}
|
||||
meta name="title" content=(title) {}
|
||||
meta property="og:title" content=(title) {}
|
||||
meta property="twitter:title" content=(title) {}
|
||||
|
||||
meta name="author" content=(author) {}
|
||||
|
||||
meta name="description" content=(description) {}
|
||||
meta property="og:description" content=(description) {}
|
||||
meta property="twitter:description" content=(description) {}
|
||||
|
||||
meta content=(image) property="og:image" {}
|
||||
link rel="shortcut icon" href=(image) type="image/x-icon" {}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl PageMetadata {
|
||||
/// Try to read page metadata from a markdown file's frontmatter.
|
||||
/// - returns `none` if there is no frontmatter
|
||||
/// - returns an error if we fail to parse frontmatter
|
||||
pub fn from_markdown_frontmatter(
|
||||
root_node: &Node,
|
||||
) -> Result<Option<PageMetadata>, serde_yaml::Error> {
|
||||
root_node
|
||||
.children
|
||||
.first()
|
||||
.and_then(|x| x.cast::<FrontMatter>())
|
||||
.map(|x| serde_yaml::from_str::<PageMetadata>(&x.content))
|
||||
.map_or(Ok(None), |v| v.map(Some))
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: page
|
||||
//
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct RequestContext {
|
||||
pub client_info: ClientInfo,
|
||||
}
|
||||
|
||||
// Some HTML
|
||||
pub struct Page {
|
||||
pub meta: PageMetadata,
|
||||
|
||||
/// How long this page's html may be cached.
|
||||
/// This controls the maximum age of a page shown to the user.
|
||||
///
|
||||
/// If `None`, this page is always rendered from scratch.
|
||||
pub html_ttl: Option<TimeDelta>,
|
||||
|
||||
/// A function that generates this page's html.
|
||||
///
|
||||
/// This should return the contents of this page's <body> tag,
|
||||
/// or the contents of a wrapper element (defined in the page server struct).
|
||||
///
|
||||
/// This closure must never return `<html>` or `<head>`.
|
||||
pub generate_html: Box<
|
||||
dyn Send
|
||||
+ Sync
|
||||
+ for<'a> Fn(
|
||||
&'a Page,
|
||||
&'a RequestContext,
|
||||
) -> Pin<Box<dyn Future<Output = Markup> + 'a + Send + Sync>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl Default for Page {
|
||||
fn default() -> Self {
|
||||
Page {
|
||||
meta: Default::default(),
|
||||
html_ttl: Some(TimeDelta::seconds(60 * 24 * 30)),
|
||||
//css_ttl: Duration::from_secs(60 * 24 * 30),
|
||||
//generate_css: None,
|
||||
generate_html: Box::new(|_, _| Box::pin(async { html!() })),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Page {
|
||||
pub async fn generate_html(&self, req_info: &RequestContext) -> Markup {
|
||||
(self.generate_html)(self, req_info).await
|
||||
}
|
||||
|
||||
pub fn from_markdown(md: impl Into<String>, default_image: Option<String>) -> Self {
|
||||
let md: String = md.into();
|
||||
let md = Markdown::parse(&md);
|
||||
|
||||
let mut meta = PageMetadata::from_markdown_frontmatter(&md)
|
||||
.unwrap_or(Some(PageMetadata {
|
||||
title: "Invalid frontmatter!".into(),
|
||||
..Default::default()
|
||||
}))
|
||||
.unwrap_or_default();
|
||||
|
||||
if meta.image.is_none() {
|
||||
meta.image = default_image
|
||||
}
|
||||
|
||||
let html = PreEscaped(md.render());
|
||||
|
||||
Page {
|
||||
meta,
|
||||
generate_html: Box::new(move |page, _| {
|
||||
let html = html.clone();
|
||||
Box::pin(async move {
|
||||
html! {
|
||||
@if let Some(slug) = &page.meta.slug {
|
||||
(Backlinks(&[("/", "home")], slug))
|
||||
}
|
||||
|
||||
(html)
|
||||
}
|
||||
})
|
||||
}),
|
||||
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: server
|
||||
//
|
||||
|
||||
// Rerender considerations:
|
||||
// - rerendering often in the background is wasteful. Maybe we should fall asleep?
|
||||
// - rerendering on request is slow
|
||||
// - rerendering in the background after a request could be a good idea. Maybe implement?
|
||||
//
|
||||
// - cached pages only make sense for static assets.
|
||||
// - user pages can't be pre-rendered!
|
||||
pub struct PageServer {
|
||||
/// If true, expired pages will be rerendered before being sent to the user.
|
||||
/// If false, requests never trigger rerenders. We rely on the rerender task.
|
||||
///
|
||||
/// If true, we deliver fresher pages but delay responses.
|
||||
/// TODO: replace this with a smarter rendering strategy?
|
||||
never_rerender_on_request: bool,
|
||||
|
||||
/// Map of `{ route: page }`
|
||||
pages: Arc<Mutex<HashMap<String, Arc<Page>>>>,
|
||||
|
||||
/// Map of `{ route: (page data, expire time) }`
|
||||
///
|
||||
/// We use an LruCache for bounded memory usage.
|
||||
html_cache: Mutex<LruCache<(String, RequestContext), (String, DateTime<Utc>)>>,
|
||||
|
||||
/// Called whenever we need to render a page.
|
||||
/// - this method should call `page.generate_html()`,
|
||||
/// - wrap the result in `<html><body>`,
|
||||
/// - and add `<head>`
|
||||
/// ```
|
||||
render_page: Box<
|
||||
dyn Send
|
||||
+ Sync
|
||||
+ for<'a> Fn(
|
||||
&'a Page,
|
||||
&'a RequestContext,
|
||||
) -> Pin<Box<dyn Future<Output = Markup> + 'a + Send + Sync>>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl PageServer {
|
||||
pub fn new(
|
||||
render_page: Box<
|
||||
dyn Send
|
||||
+ Sync
|
||||
+ for<'a> Fn(
|
||||
&'a Page,
|
||||
&'a RequestContext,
|
||||
) -> Pin<Box<dyn Future<Output = Markup> + 'a + Send + Sync>>,
|
||||
>,
|
||||
) -> Arc<Self> {
|
||||
#[expect(clippy::unwrap_used)]
|
||||
let cache_size = NonZero::new(128).unwrap();
|
||||
|
||||
Arc::new(Self {
|
||||
pages: Arc::new(Mutex::new(HashMap::new())),
|
||||
html_cache: Mutex::new(LruCache::new(cache_size)),
|
||||
render_page,
|
||||
never_rerender_on_request: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn add_page(&self, route: impl Into<String>, page: Page) -> &Self {
|
||||
#[expect(clippy::expect_used)]
|
||||
let route = route
|
||||
.into()
|
||||
.strip_prefix("/")
|
||||
.expect("page route must start with /")
|
||||
.to_owned();
|
||||
|
||||
self.pages.lock().insert(route, Arc::new(page));
|
||||
self
|
||||
}
|
||||
|
||||
/// Re-render the page at `route`, regardless of cache state.
|
||||
/// Does nothing if there is no page at `route`.
|
||||
///
|
||||
/// Returns the rendered page's content.
|
||||
async fn render_page(
|
||||
&self,
|
||||
reason: &'static str,
|
||||
route: &str,
|
||||
req_ctx: RequestContext,
|
||||
) -> Option<(String, Option<DateTime<Utc>>)> {
|
||||
let now = Utc::now();
|
||||
let start = Instant::now();
|
||||
trace!(message = "Rendering page", route, reason);
|
||||
|
||||
let page = match self.pages.lock().get(route) {
|
||||
Some(x) => x.clone(),
|
||||
None => {
|
||||
warn!(message = "Not rerendering, no such route", route, reason);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let html = (self.render_page)(&page, &req_ctx).await.0;
|
||||
|
||||
let mut expires = None;
|
||||
if let Some(ttl) = page.html_ttl {
|
||||
expires = Some(now + ttl);
|
||||
self.html_cache
|
||||
.lock()
|
||||
.put((route.to_owned(), req_ctx), (html.clone(), now + ttl));
|
||||
}
|
||||
|
||||
let elapsed = start.elapsed().as_millis();
|
||||
trace!(message = "Rendered page", route, reason, time_ms = elapsed);
|
||||
return Some((html, expires));
|
||||
}
|
||||
|
||||
async fn handler(
|
||||
Path(route): Path<String>,
|
||||
State(state): State<Arc<Self>>,
|
||||
ConnectInfo(addr): ConnectInfo<ServiceConnectInfo>,
|
||||
headers: HeaderMap,
|
||||
) -> Response {
|
||||
let client_info = ClientInfo::from_headers(&headers);
|
||||
let ua = headers
|
||||
.get("user-agent")
|
||||
.and_then(|x| x.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
trace!(
|
||||
message = "Serving route",
|
||||
route,
|
||||
addr = ?addr.addr,
|
||||
user_agent = ua,
|
||||
device_type = ?client_info.device_type
|
||||
);
|
||||
|
||||
let req_ctx = RequestContext { client_info };
|
||||
|
||||
let cache_key = (route.clone(), req_ctx.clone());
|
||||
let now = Utc::now();
|
||||
let mut html_expires = None;
|
||||
|
||||
// Get from cache, if availablee
|
||||
if let Some((html, expires)) = state.html_cache.lock().get(&cache_key)
|
||||
&& (*expires > now || state.never_rerender_on_request)
|
||||
{
|
||||
html_expires = Some((html.clone(), Some(*expires)));
|
||||
};
|
||||
|
||||
if html_expires.is_none() {
|
||||
html_expires = match state.render_page("request", &route, req_ctx).await {
|
||||
Some(x) => Some(x.clone()),
|
||||
None => return (StatusCode::NOT_FOUND, "page doesn't exist").into_response(),
|
||||
};
|
||||
}
|
||||
|
||||
#[expect(clippy::unwrap_used)]
|
||||
let (html, expires) = html_expires.unwrap();
|
||||
|
||||
let mut headers = HeaderMap::with_capacity(3);
|
||||
headers.append(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("text/html; charset=utf-8"),
|
||||
);
|
||||
|
||||
let max_age = match expires {
|
||||
Some(expires) => (expires - now).num_seconds().max(1),
|
||||
None => 1,
|
||||
};
|
||||
|
||||
#[expect(clippy::unwrap_used)]
|
||||
headers.append(
|
||||
header::CACHE_CONTROL,
|
||||
// immutable; public/private
|
||||
HeaderValue::from_str(&format!("immutable, public, max-age={}", max_age)).unwrap(),
|
||||
);
|
||||
|
||||
headers.append("Accept-CH", HeaderValue::from_static("Sec-CH-UA-Mobile"));
|
||||
|
||||
return (headers, html).into_response();
|
||||
}
|
||||
|
||||
pub fn into_router(self: Arc<Self>) -> Router<()> {
|
||||
let compression: CompressionLayer = CompressionLayer::new()
|
||||
.br(true)
|
||||
.deflate(true)
|
||||
.gzip(true)
|
||||
.zstd(true)
|
||||
.compress_when(DefaultPredicate::new());
|
||||
|
||||
Router::new()
|
||||
.route(
|
||||
"/",
|
||||
get(|state, conn, headers| async {
|
||||
Self::handler(Path(String::new()), state, conn, headers).await
|
||||
}),
|
||||
)
|
||||
.route("/{*path}", get(Self::handler))
|
||||
.layer(compression)
|
||||
.with_state(self)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// MARK: UserAgent
|
||||
//
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum DeviceType {
|
||||
Mobile,
|
||||
Desktop,
|
||||
}
|
||||
|
||||
impl Default for DeviceType {
|
||||
fn default() -> Self {
|
||||
Self::Desktop
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ClientInfo {
|
||||
/// This is an estimate, but it's probably good enough.
|
||||
pub device_type: DeviceType,
|
||||
}
|
||||
|
||||
impl ClientInfo {
|
||||
pub fn from_headers(headers: &HeaderMap) -> Self {
|
||||
let ua = headers
|
||||
.get("user-agent")
|
||||
.and_then(|x| x.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
let ch_mobile = headers
|
||||
.get("Sec-CH-UA-Mobile")
|
||||
.and_then(|x| x.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
let mut device_type = None;
|
||||
|
||||
if device_type.is_none() && ch_mobile.contains("1") {
|
||||
device_type = Some(DeviceType::Mobile);
|
||||
}
|
||||
|
||||
if device_type.is_none() && ua.contains("Mobile") {
|
||||
device_type = Some(DeviceType::Mobile);
|
||||
}
|
||||
|
||||
Self {
|
||||
device_type: device_type.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,16 +7,16 @@ use std::{
|
||||
use assetserver::Asset;
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use maud::{Markup, PreEscaped, html};
|
||||
use page::{DeviceType, Page, RequestContext};
|
||||
use parking_lot::Mutex;
|
||||
use serde::Deserialize;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
md::Markdown,
|
||||
md::{Markdown, meta_from_markdown},
|
||||
misc::{Backlinks, FarLink},
|
||||
},
|
||||
page::{DeviceType, Page, PageMetadata, RequestContext},
|
||||
routes::assets::Image_Icon,
|
||||
};
|
||||
|
||||
@@ -190,9 +190,7 @@ pub fn handouts() -> Page {
|
||||
tokio::spawn(index.clone().autoget(Duration::from_secs(60 * 20)));
|
||||
|
||||
#[expect(clippy::unwrap_used)]
|
||||
let mut meta = PageMetadata::from_markdown_frontmatter(&md)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let mut meta = meta_from_markdown(&md).unwrap().unwrap();
|
||||
|
||||
if meta.image.is_none() {
|
||||
meta.image = Some(Image_Icon::URL.to_owned());
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use assetserver::Asset;
|
||||
use maud::html;
|
||||
use page::{Page, PageMetadata};
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
@@ -8,7 +9,6 @@ use crate::{
|
||||
md::Markdown,
|
||||
misc::FarLink,
|
||||
},
|
||||
page::{Page, PageMetadata},
|
||||
routes::assets::{Image_Cover, Image_Icon},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use assetserver::Asset;
|
||||
use page::Page;
|
||||
|
||||
use crate::{page::Page, routes::assets::Image_Icon};
|
||||
use crate::{components::md::page_from_markdown, routes::assets::Image_Icon};
|
||||
|
||||
mod handouts;
|
||||
mod index;
|
||||
@@ -16,11 +17,11 @@ pub fn links() -> Page {
|
||||
http://www.3dprintmath.com/
|
||||
*/
|
||||
|
||||
Page::from_markdown(include_str!("links.md"), Some(Image_Icon::URL.to_owned()))
|
||||
page_from_markdown(include_str!("links.md"), Some(Image_Icon::URL.to_owned()))
|
||||
}
|
||||
|
||||
pub fn betalupi() -> Page {
|
||||
Page::from_markdown(
|
||||
page_from_markdown(
|
||||
include_str!("betalupi.md"),
|
||||
Some(Image_Icon::URL.to_owned()),
|
||||
)
|
||||
|
||||
@@ -3,14 +3,10 @@ use std::{pin::Pin, sync::Arc};
|
||||
use assetserver::Asset;
|
||||
use axum::Router;
|
||||
use maud::{DOCTYPE, Markup, PreEscaped, html};
|
||||
use page::{Page, PageServer, RequestContext};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
components::misc::FarLink,
|
||||
page::{Page, PageServer, RequestContext},
|
||||
pages,
|
||||
routes::assets::Styles_Main,
|
||||
};
|
||||
use crate::{components::misc::FarLink, pages, routes::assets::Styles_Main};
|
||||
|
||||
pub mod assets;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user