samjeston / openhub Public
Sam Jeston Working vertical 9aa3c45 · History
54 lines· 1.3 KB ·Rust
Raw
1//! Static assets compiled into the binary.
2
3use axum::http::header;
4use axum::response::IntoResponse;
5
6const STYLE: &str = include_str!("../../static/style.css");
7const SCRIPT: &str = include_str!("../../static/app.js");
8const FAVICON: &str = include_str!("../../static/favicon.svg");
9
10const CACHE: &str = "public, max-age=3600";
11
12pub async fn stylesheet() -> impl IntoResponse {
13 (
14 [
15 (header::CONTENT_TYPE, "text/css; charset=utf-8"),
16 (header::CACHE_CONTROL, CACHE),
17 ],
18 STYLE,
19 )
20}
21
22pub async fn script() -> impl IntoResponse {
23 (
24 [
25 (header::CONTENT_TYPE, "text/javascript; charset=utf-8"),
26 (header::CACHE_CONTROL, CACHE),
27 ],
28 SCRIPT,
29 )
30}
31
32pub async fn favicon() -> impl IntoResponse {
33 (
34 [
35 (header::CONTENT_TYPE, "image/svg+xml"),
36 (header::CACHE_CONTROL, CACHE),
37 ],
38 FAVICON,
39 )
40}
41
42/// Short content hash appended to asset URLs so browsers pick up new versions.
43pub fn version() -> &'static str {
44 use sha2::{Digest, Sha256};
45 use std::sync::LazyLock;
46 static VERSION: LazyLock<String> = LazyLock::new(|| {
47 let digest = Sha256::new()
48 .chain_update(STYLE)
49 .chain_update(SCRIPT)
50 .finalize();
51 hex::encode(&digest[..4])
52 });
53 &VERSION
54}