Build option for a base url
parent
c371008360
commit
a53d7887fd
|
@ -3,6 +3,7 @@
|
||||||
debug/
|
debug/
|
||||||
target/
|
target/
|
||||||
out/
|
out/
|
||||||
|
.env
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
# 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
|
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||||
|
|
7
justfile
7
justfile
|
@ -1,7 +1,12 @@
|
||||||
set export
|
set export
|
||||||
set quiet
|
set quiet
|
||||||
|
set dotenv-load
|
||||||
|
|
||||||
build:
|
build publish="false":
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
if [ "{{publish}}" = false ]; then
|
||||||
|
export BASE_URL=""
|
||||||
|
fi
|
||||||
mkdir -p ./out
|
mkdir -p ./out
|
||||||
rm -rf ./out/*
|
rm -rf ./out/*
|
||||||
cargo run
|
cargo run
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -1,6 +1,11 @@
|
||||||
#![allow(dead_code)]
|
#![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 askama::Template;
|
||||||
use blake3::{Hash, Hasher};
|
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 ASSETS_FOLDER_PATH: &str = "./public/assets";
|
||||||
pub const SCRIPTS_FOLDER_PATH: &str = "./public/scripts";
|
pub const SCRIPTS_FOLDER_PATH: &str = "./public/scripts";
|
||||||
pub const STYLES_FOLDER_PATH: &str = "./public/styles";
|
pub const STYLES_FOLDER_PATH: &str = "./public/styles";
|
||||||
|
static BASE_URL: OnceLock<String> = OnceLock::new();
|
||||||
static FILE_HASHES: LazyLock<HashMap<String, Hash>> = LazyLock::new(|| {
|
static FILE_HASHES: LazyLock<HashMap<String, Hash>> = LazyLock::new(|| {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
|
|
||||||
|
@ -106,14 +112,22 @@ fn start_module<M: RuntimeModule>() {
|
||||||
M::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 {
|
fn find_hash(file: &str) -> String {
|
||||||
let map = &*FILE_HASHES;
|
let map = &*FILE_HASHES;
|
||||||
|
let base_url = base_url();
|
||||||
|
|
||||||
let hash = map.get(&format!(".{file}"));
|
let hash = map.get(&format!(".{file}"));
|
||||||
if let Some(hash) = hash {
|
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) {
|
fn hash_files_in_dir(path: PathBuf, hasher: &mut Hasher) {
|
||||||
|
|
Loading…
Reference in New Issue