From a53d7887fd597d1850c64302a12f7809401ebd7e Mon Sep 17 00:00:00 2001 From: Wynd Date: Mon, 30 Jun 2025 00:18:20 +0300 Subject: [PATCH] Build option for a base url --- .gitignore | 1 + justfile | 7 ++++++- src/main.rs | 20 +++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 55b30b8..e8ae35e 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/justfile b/justfile index 722e4e3..2b2ab82 100644 --- a/justfile +++ b/justfile @@ -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 diff --git a/src/main.rs b/src/main.rs index 64821c3..341ec38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = OnceLock::new(); static FILE_HASHES: LazyLock> = LazyLock::new(|| { let mut map = HashMap::new(); @@ -106,14 +112,22 @@ fn start_module() { 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) {