use laravel::http::{Request, Response, StatusCode, header};
pub(crate) struct Asset {
bytes: &'static [u8],
content_type: &'static str,
hash: u64,
}
impl Asset {
const fn new(bytes: &'static [u8], content_type: &'static str) -> Self {
let mut hash = 0xcbf2_9ce4_8422_2325_u64;
let mut at = 0;
while at < bytes.len() {
hash = (hash ^ bytes[at] as u64).wrapping_mul(0x0100_0000_01b3);
at += 1;
}
Self {
bytes,
content_type,
hash,
}
}
pub(crate) fn version(&self) -> String {
format!("{:016x}", self.hash)
}
fn serve(&self, request: &Request) -> Response {
let tag = format!("\"{}\"", self.version());
let held = request
.header(header::IF_NONE_MATCH)
.is_some_and(|tags| tags.split(',').map(str::trim).any(|held| held == "*" || held == tag));
let response = if held {
Response::new(StatusCode::NOT_MODIFIED)
} else {
Response::new(self.bytes)
};
response.with_headers([
(header::CONTENT_TYPE, self.content_type),
(header::CACHE_CONTROL, "private, no-cache"),
(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
(header::ETAG, tag.as_str()),
])
}
}
const SCRIPT: &str = "text/javascript; charset=utf-8";
pub(crate) static LAUNCHER: Asset = Asset::new(include_bytes!("../../dist/launcher.js"), SCRIPT);
pub(crate) static PANEL_SCRIPT: Asset = Asset::new(include_bytes!("../../dist/panel.js"), SCRIPT);
pub(crate) static PANEL_STYLES: Asset = Asset::new(include_bytes!("../../dist/panel.css"), "text/css; charset=utf-8");
pub(crate) async fn launcher(request: Request) -> Response {
LAUNCHER.serve(&request)
}
pub(crate) async fn panel_script(request: Request) -> Response {
PANEL_SCRIPT.serve(&request)
}
pub(crate) async fn panel_styles(request: Request) -> Response {
PANEL_STYLES.serve(&request)
}