Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions crates/bin/docs_rs_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ build = "build.rs"

[package.metadata.cargo-machete]
ignored = [
"phf",
# used in build script output
"slug", # used in askama templates, can be moved to web binary
]
Expand Down Expand Up @@ -51,7 +50,6 @@ lol_html = "2.0.0"
mime = { workspace = true }
num_cpus = "1.15.0"
opentelemetry = { workspace = true }
phf = "0.13.1"
postcard = { workspace = true }
rayon-core = "1.13.0"
regex = { workspace = true }
Expand All @@ -76,7 +74,6 @@ url = { workspace = true }
anyhow = { version = "1.0.42", features = ["backtrace"] }
grass = { version = "0.13.1", default-features = false }
md5 = "0.8.0"
phf_codegen = "0.13"
syntect = { version = "5.0.0", default-features = false, features = ["parsing", "dump-create", "yaml-load", "regex-onig"] }
walkdir = { workspace = true }

Expand Down
24 changes: 13 additions & 11 deletions crates/bin/docs_rs_web/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context as _, Error, Result};
use std::{env, fs::File, io::Write as _, path::Path};
use std::{collections::BTreeMap, env, fs::File, io::Write as _, path::Path};

mod tracked {
use std::{
Expand Down Expand Up @@ -68,7 +68,7 @@ mod tracked {
}
}

type ETagMap<'a> = phf_codegen::Map<'a, String>;
type ETagMap = BTreeMap<String, String>;

fn main() -> Result<()> {
let out_dir = env::var("OUT_DIR").context("missing OUT_DIR")?;
Expand All @@ -81,11 +81,13 @@ fn main() -> Result<()> {
calculate_static_etags(&mut etag_map)?;

let mut etag_file = File::create(out_dir.join("static_etag_map.rs"))?;
writeln!(
&mut etag_file,
"pub static STATIC_ETAG_MAP: ::phf::Map<&'static str, &'static str> = {};",
etag_map.build()
)?;
writeln!(etag_file, "pub const STATIC_ETAG_MAP: &[(&str, &str)] = &[")?;
for (path, etag) in etag_map.iter() {
// the debug repr of a `str` is also a valid escaped string literal in the code
writeln!(etag_file, r#" ({:?}, {:?}), "#, path, etag)?;
}
writeln!(etag_file, "];")?;

etag_file.sync_all()?;

// trigger recompilation when a new migration is added
Expand All @@ -100,7 +102,7 @@ fn etag_from_path(path: impl AsRef<Path>) -> Result<String> {
fn etag_from_content(content: impl AsRef<[u8]>) -> String {
let digest = md5::compute(content);
let md5_hex = format!("{:x}", digest);
format!(r#""\"{md5_hex}\"""#)
format!(r#""{md5_hex}""#)
}

fn compile_sass_file(src: &Path, dest: &Path) -> Result<()> {
Expand Down Expand Up @@ -137,7 +139,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
})?;

let dest_str = dest.file_name().unwrap().to_str().unwrap().to_owned();
etag_map.entry(dest_str, etag_from_path(&dest)?);
etag_map.insert(dest_str, etag_from_path(&dest)?);
}
}
}
Expand All @@ -148,7 +150,7 @@ fn compile_sass(out_dir: &Path, etag_map: &mut ETagMap) -> Result<()> {
let vendored = pure + &grids;
std::fs::write(out_dir.join("vendored").with_extension("css"), &vendored)?;

etag_map.entry(
etag_map.insert(
"vendored.css".to_owned(),
etag_from_content(vendored.as_bytes()),
);
Expand All @@ -169,7 +171,7 @@ fn calculate_static_etags(etag_map: &mut ETagMap) -> Result<()> {

let partial_path = path.strip_prefix(static_dir).unwrap();
let partial_path_str = partial_path.to_string_lossy().to_string();
etag_map.entry(partial_path_str, etag_from_path(path)?);
etag_map.insert(partial_path_str, etag_from_path(path)?);
}
}

Expand Down
15 changes: 11 additions & 4 deletions crates/bin/docs_rs_web/src/handlers/statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,19 @@ async fn conditional_get(
req: Request,
next: Next,
) -> Response {
debug_assert!(STATIC_ETAG_MAP.is_sorted());

let if_none_match = if_none_match.map(|th| th.0);
let resource_path = partial_uri.path().trim_start_matches('/');
let Some(etag) = STATIC_ETAG_MAP.get(resource_path).map(|etag| {
etag.parse::<ETag>()
.expect("compile time generated, should always pass")
}) else {
let Some(etag) = STATIC_ETAG_MAP
.binary_search_by_key(&resource_path, |(path, _)| *path)
.ok()
.map(|pos| {
let etag = STATIC_ETAG_MAP[pos].1;
etag.parse::<ETag>()
.expect("compile time generated, should always pass")
})
else {
let res = next.run(req).await;

debug_assert!(
Expand Down
Loading