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,
};
})
}
}