From 1b79a07c5b310c23dad0f4189dc8c1ff930d7474 Mon Sep 17 00:00:00 2001 From: august kline Date: Thu, 14 Nov 2024 02:30:41 -0500 Subject: [PATCH] Moved initial asset fetching to compile time --- build.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++ src/html.rs | 1 + src/server/mod.rs | 28 +++---------------------- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/build.rs b/build.rs index ba5f286..3ca86c5 100644 --- a/build.rs +++ b/build.rs @@ -53,4 +53,56 @@ fn main() { map.build() ) .unwrap(); + + // Get slug and absolute path of all assets + // In our actual source: + // ``` + // let assets: Vec<(String, Vec)> = vec![] + // for (slug, path) in ASSET_PATHS { + // let asset = (slug, include_bytes!(path)); + // assets.push(asset); + // } + // db.add_assets(assets).await? + // ``` + let assets_dir = [ + &env::var("CARGO_MANIFEST_DIR").unwrap(), + "/src/client/assets", + ] + .concat(); + println!("cargo:rerun-if-changed={}", assets_dir); + let pattern = [&assets_dir, "/**/*.*"].concat(); + let paths = glob(&pattern).unwrap(); + let path = Path::new(&env::var("OUT_DIR").unwrap()).join("add_assets.rs"); + let mut file = BufWriter::new(File::create(&path).unwrap()); + + writeln!( + &mut file, + r#"{{let mut assets: Vec<(&str, &[u8])> = vec![];"# + ) + .unwrap(); + let mut ident_name = String::new(); + for path in paths { + match path { + Ok(path) => { + ident_name.push('a'); + let slug_path = path.clone(); + let slug: String = slug_path + .strip_prefix(&assets_dir) + .unwrap() + .as_os_str() + .to_str() + .unwrap() + .into(); // eek + println!("cargo:warning={}{:?}", &slug, &path); + writeln!( + &mut file, + r#"assets.push(("{slug}",include_bytes!("{}")));"#, + path.to_str().unwrap() + ) + .unwrap(); + } + Err(_) => todo!(), + } + } + writeln!(&mut file, r#"db.add_assets(assets).await?;}}"#).unwrap(); } diff --git a/src/html.rs b/src/html.rs index a81a4b4..5853b34 100644 --- a/src/html.rs +++ b/src/html.rs @@ -35,6 +35,7 @@ pub(crate) fn make_page(content: S, settings: PageSettings) -> String where S: AsRef, { + for t in TEMPLATES.into_iter() {} rewrite_str( template!("default"), RewriteStrSettings { diff --git a/src/server/mod.rs b/src/server/mod.rs index b0a7bf9..e5bde6a 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -31,6 +31,7 @@ use blogdb::BlogDb; use glob::glob; use crate::html; +include!(concat!(env!("OUT_DIR"), "/codegen.rs")); const DB_URL: &str = "sqlite://evie.db"; @@ -146,35 +147,12 @@ impl BlogStateInner { Ok(()) } - const ASSETS_DIR: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/src/client/assets"); - async fn add_initial_assets(db: &BlogDb) -> anyhow::Result<()> { - let pattern = [Self::ASSETS_DIR, "/*/*"].concat(); - let paths = glob(&pattern).unwrap(); - let mut assets = vec![]; - for path in paths.into_iter() { - match path { - Ok(path) => { - let slug_path = path.clone(); - let slug: String = slug_path - .strip_prefix(Self::ASSETS_DIR)? - .as_os_str() - .to_str() - .unwrap() - .into(); // eek - let mut file = File::open(path.clone())?; - let mut data = vec![]; - file.read_to_end(&mut data).unwrap(); - assets.push((slug, data)); - } - Err(_) => todo!(), - } - } - db.add_assets(assets).await?; + include!(concat!(env!("OUT_DIR"), "/add_assets.rs")); Ok(()) } - const INDEX_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/index"); + const INDEX_PATH: &'static str = "./index"; const MEMORY_BUDGET: usize = 50_000_000; async fn generate_index(db: &BlogDb) -> anyhow::Result<(Index, IndexWriter)> {