Moved initial asset fetching to compile time

This commit is contained in:
august kline 2024-11-14 02:30:41 -05:00
parent a8c0bb1641
commit 1b79a07c5b
3 changed files with 56 additions and 25 deletions

View File

@ -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<u8>)> = 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();
}

View File

@ -35,6 +35,7 @@ pub(crate) fn make_page<S>(content: S, settings: PageSettings) -> String
where
S: AsRef<str>,
{
for t in TEMPLATES.into_iter() {}
rewrite_str(
template!("default"),
RewriteStrSettings {

View File

@ -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)> {