Sam Jeston
History
54 lines·
1.3 KB
·Rust
| 1 | //! Static assets compiled into the binary. |
| 2 | |
| 3 | use axum::http::header; |
| 4 | use axum::response::IntoResponse; |
| 5 | |
| 6 | const STYLE: &str = include_str!("../../static/style.css"); |
| 7 | const SCRIPT: &str = include_str!("../../static/app.js"); |
| 8 | const FAVICON: &str = include_str!("../../static/favicon.svg"); |
| 9 | |
| 10 | const CACHE: &str = "public, max-age=3600"; |
| 11 | |
| 12 | pub 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 | |
| 22 | pub 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 | |
| 32 | pub 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. |
| 43 | pub 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 | } |