diff --git a/Cargo.lock b/Cargo.lock index 58f1140..ef9899f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,6 +238,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + [[package]] name = "bumpalo" version = "3.19.0" @@ -448,6 +457,7 @@ version = "0.1.0" dependencies = [ "askama", "axum", + "bs58", "color-eyre", "futures", "jiff", @@ -456,6 +466,7 @@ dependencies = [ "reqwest", "rootcause", "serde", + "sha2", "sqlx", "tempfile", "tokio", diff --git a/Cargo.toml b/Cargo.toml index ddff61d..96026d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,4 +31,6 @@ tracing = { version = "0.1.40", features = ["attributes"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } [build-dependencies] +bs58 = "0.5.1" color-eyre = "0.6.3" +sha2 = "0.10.9" diff --git a/build.rs b/build.rs index 945d25a..d5d1eb6 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,29 @@ +use std::path::PathBuf; + +use sha2::Digest; + fn main() { // Always rerun. + let index_css = include_str!("static/index.css"); + let index_js = include_str!("static/index.js"); + + let index_css_name = get_file_ref("css", index_css); + let index_js_name = get_file_ref("js", index_js); + + let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); + + std::fs::write( + out_dir.join("revs.rs"), + format!( + r#" +pub const INDEX_CSS_NAME: &str = "{index_css_name}"; +pub const INDEX_JS_NAME: &str = "{index_js_name}"; + "# + ), + ) + .unwrap(); + let version = if let Ok(commit) = try_get_commit() { match has_no_changes() { Ok(true) => commit, @@ -20,6 +43,15 @@ fn main() { println!("cargo:rustc-env=GIT_COMMIT_SHORT={version_short}"); } +fn get_file_ref(ext: &str, content: &str) -> String { + let mut hash = sha2::Sha256::new(); + hash.update(content); + let digest = hash.finalize(); + let rev = bs58::encode(digest).into_string(); + let rev = &rev[..16]; + format!("/{rev}.{ext}") +} + fn try_get_commit() -> color_eyre::Result { if let Ok(overridden) = std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION") { return Ok(overridden); diff --git a/src/web.rs b/src/web.rs index db2a22d..be07855 100644 --- a/src/web.rs +++ b/src/web.rs @@ -16,6 +16,10 @@ use crate::{ notification, Result, }; +mod revs { + include!(concat!(env!("OUT_DIR"), "/revs.rs")); +} + #[derive(Clone)] pub struct AppState { pub db: Db, @@ -28,8 +32,8 @@ pub async fn webserver(db: Db, notification_repo: String) -> Result<()> { .route("/build", get(web_build)) .route("/target", get(web_target)) .route("/nightly", get(web_nightly)) - .route("/index.css", get(index_css)) - .route("/index.js", get(index_js)) + .route(revs::INDEX_CSS_NAME, get(index_css)) + .route(revs::INDEX_JS_NAME, get(index_js)) .with_state(AppState { db, notification_repo, @@ -334,23 +338,33 @@ async fn web_root(State(state): State) -> impl IntoResponse { }) } -async fn index_css() -> impl IntoResponse { +fn reply_static(body: &'static str, content_type: &'static str) -> impl IntoResponse { ( - [( - axum::http::header::CONTENT_TYPE, - axum::http::HeaderValue::from_static("text/css; charset=utf-8"), - )], + [ + ( + axum::http::header::CONTENT_TYPE, + axum::http::HeaderValue::from_static(content_type), + ), + ( + axum::http::header::CACHE_CONTROL, + axum::http::HeaderValue::from_static("public, max-age=31556952, immutable"), + ), + ], + body, + ) +} + +async fn index_css() -> impl IntoResponse { + reply_static( include_str!("../static/index.css"), + "text/css; charset=utf-8", ) } async fn index_js() -> impl IntoResponse { - ( - [( - axum::http::header::CONTENT_TYPE, - axum::http::HeaderValue::from_static("application/javascript; charset=utf-8"), - )], + reply_static( include_str!("../static/index.js"), + "application/javascript; charset=utf-8", ) } diff --git a/templates/build.html b/templates/build.html index 3a5389d..2e1ad85 100644 --- a/templates/build.html +++ b/templates/build.html @@ -4,8 +4,8 @@ Build {{nightly}} {{target}} - - + +

diff --git a/templates/index.html b/templates/index.html index e6084c0..6fa97d9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,8 +4,8 @@ Does it build? - - + +

Does it build?

diff --git a/templates/nightly.html b/templates/nightly.html index cc5d5f1..e793744 100644 --- a/templates/nightly.html +++ b/templates/nightly.html @@ -4,7 +4,7 @@ {{nightly}} build history - +

Nightly build state for {{nightly}}

diff --git a/templates/target.html b/templates/target.html index b2df511..c94c06e 100644 --- a/templates/target.html +++ b/templates/target.html @@ -4,7 +4,7 @@ {{target}} build history - +

Target build history for {{target}}