61 lines
1.7 KiB
Rust
61 lines
1.7 KiB
Rust
|
|
use std::borrow::Cow;
|
|
use crate::Response;
|
|
|
|
const DEFAULT_CACHE: &[u8] = b"max-age=86400";
|
|
|
|
/// Represents a single static asset
|
|
///
|
|
/// Each asset is embeded into the binary, and can be served under the
|
|
/// `/static/{filename}` route. All static assets should be embeded in this way, and can
|
|
/// be referenced from this module.
|
|
pub struct StaticAsset {
|
|
|
|
/// The filename of this asset relative to `:/web/assets/` and the `/static/` route
|
|
pub filename: &'static str,
|
|
|
|
/// The content of the file
|
|
pub bytes: &'static [u8],
|
|
|
|
}
|
|
|
|
impl StaticAsset {
|
|
const STATIC_HEADERS: &'static [(&'static [u8], Cow<'static, [u8]>)] = &[
|
|
(b"Cache-Control", Cow::Borrowed(DEFAULT_CACHE)),
|
|
(b"Trans-People", Cow::Borrowed(b"Are Gods")),
|
|
];
|
|
|
|
/// Generate the HTTP response for sending this asset to the client
|
|
// I wrote all this code to make this a const fn, and then don't even use it in
|
|
// compile-time :(
|
|
pub const fn generate_response(&self) -> Response {
|
|
Response {
|
|
status: b"200",
|
|
headers: Cow::Borrowed(StaticAsset::STATIC_HEADERS),
|
|
body: Cow::Borrowed(self.bytes),
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// Generate a [`StaticAsset`] at compiletime from a filename.
|
|
#[cfg(any(feature = "embed_static_assets", feature = "ogp_images"))]
|
|
macro_rules! static_asset {
|
|
( $filename: expr ) => {
|
|
StaticAsset {
|
|
filename: $filename,
|
|
bytes: include_bytes!(concat!("../assets/", $filename)),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The decorative font to be used for pronouns displayed at a very large size
|
|
#[cfg(any(feature = "embed_static_assets", feature = "ogp_images"))]
|
|
pub const FONT: StaticAsset = static_asset!("font.otf");
|
|
|
|
/// A list of static assets which should be served by the server
|
|
pub const STATIC_ASSETS: &[&StaticAsset] = &[
|
|
#[cfg(any(feature = "embed_static_assets"))]
|
|
&FONT,
|
|
];
|