Merge asset and page server

This commit is contained in:
2025-11-08 09:33:12 -08:00
parent 6cb54c2300
commit e70170ee5b
18 changed files with 279 additions and 621 deletions

View File

@@ -0,0 +1,44 @@
use axum::http::{
HeaderMap, HeaderValue, StatusCode,
header::{self},
};
use std::pin::Pin;
use toolbox::mime::MimeType;
use crate::{Rendered, RenderedBody, RequestContext, Servable};
pub struct StaticAsset {
pub bytes: &'static [u8],
pub mime: MimeType,
}
impl Servable for StaticAsset {
fn render<'a>(
&'a self,
_ctx: &'a RequestContext,
) -> Pin<Box<dyn Future<Output = crate::Rendered> + 'a + Send + Sync>> {
Box::pin(async {
let mut headers = HeaderMap::with_capacity(3);
#[expect(clippy::unwrap_used)]
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_str(&self.mime.to_string()).unwrap(),
);
headers.insert(
header::CACHE_CONTROL,
HeaderValue::from_str(&format!("immutable, public, max-age={}", 60 * 60 * 24 * 30))
.unwrap(),
);
return Rendered {
code: StatusCode::OK,
headers,
body: RenderedBody::Static(self.bytes),
ttl: None,
immutable: true,
};
})
}
}

View File

@@ -1,2 +1,3 @@
pub mod asset;
pub mod page;
pub mod redirect;

View File

@@ -7,7 +7,7 @@ use maud::{Markup, Render, html};
use serde::Deserialize;
use std::pin::Pin;
use crate::{Rendered, RequestContext, Servable};
use crate::{Rendered, RenderedBody, RequestContext, Servable};
//
// MARK: metadata
@@ -67,6 +67,7 @@ impl Render for PageMetadata {
// Some HTML
pub struct Page {
pub meta: PageMetadata,
pub immutable: bool,
/// How long this page's html may be cached.
/// This controls the maximum age of a page shown to the user.
@@ -94,10 +95,11 @@ 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),
html_ttl: Some(TimeDelta::seconds(60 * 60 * 24 * 30)),
//css_ttl: Duration::from_secs(60 * 60 * 24 * 30),
//generate_css: None,
generate_html: Box::new(|_, _| Box::pin(async { html!() })),
immutable: true,
}
}
}
@@ -125,8 +127,9 @@ impl Servable for Page {
return Rendered {
code: StatusCode::OK,
headers,
body: html.0.into_bytes(),
body: RenderedBody::Markup(html),
ttl: self.html_ttl,
immutable: self.immutable,
};
})
}

View File

@@ -5,7 +5,7 @@ use axum::http::{
header::{self, InvalidHeaderValue},
};
use crate::{Rendered, RequestContext, Servable};
use crate::{Rendered, RenderedBody, RequestContext, Servable};
pub struct Redirect {
to: HeaderValue,
@@ -31,8 +31,9 @@ impl Servable for Redirect {
return Rendered {
code: StatusCode::PERMANENT_REDIRECT,
headers,
body: Vec::new(),
body: RenderedBody::Empty,
ttl: None,
immutable: true,
};
})
}