Build option for a base url

master
Wynd 2025-06-30 00:18:20 +03:00
parent c371008360
commit a53d7887fd
3 changed files with 24 additions and 4 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
debug/
target/
out/
.env
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

View File

@ -1,7 +1,12 @@
set export
set quiet
set dotenv-load
build:
build publish="false":
#!/usr/bin/env bash
if [ "{{publish}}" = false ]; then
export BASE_URL=""
fi
mkdir -p ./out
rm -rf ./out/*
cargo run

View File

@ -1,6 +1,11 @@
#![allow(dead_code)]
use std::{collections::HashMap, fs, path::PathBuf, sync::LazyLock};
use std::{
collections::HashMap,
fs,
path::PathBuf,
sync::{LazyLock, OnceLock},
};
use askama::Template;
use blake3::{Hash, Hasher};
@ -27,6 +32,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const ASSETS_FOLDER_PATH: &str = "./public/assets";
pub const SCRIPTS_FOLDER_PATH: &str = "./public/scripts";
pub const STYLES_FOLDER_PATH: &str = "./public/styles";
static BASE_URL: OnceLock<String> = OnceLock::new();
static FILE_HASHES: LazyLock<HashMap<String, Hash>> = LazyLock::new(|| {
let mut map = HashMap::new();
@ -106,14 +112,22 @@ fn start_module<M: RuntimeModule>() {
M::start_module();
}
fn base_url() -> String {
BASE_URL
.get_or_init(|| std::env::var("BASE_URL").unwrap_or("".to_string()))
.to_string()
}
fn find_hash(file: &str) -> String {
let map = &*FILE_HASHES;
let base_url = base_url();
let hash = map.get(&format!(".{file}"));
if let Some(hash) = hash {
return format!("{file}?hash={hash}");
return format!("{base_url}{file}?hash={hash}");
}
file.to_string()
format!("{base_url}{file}")
}
fn hash_files_in_dir(path: PathBuf, hasher: &mut Hasher) {