Skip to content

Commit

Permalink
Implement web_root config (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored May 13, 2024
1 parent a9272f7 commit 580bf8c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
11 changes: 10 additions & 1 deletion website/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ fn hash(value: &[u8]) -> String {

impl AssetPaths {
pub fn new(config: &Config) -> AssetPaths {
let dir = OutDir::new("assets_static");
let dir = OutDir::new_path(
Path::new(&config.web_root)
.join("assets_static")
.strip_prefix("/")
.unwrap(),
);

let root_index = config.web_root.clone();

let style_css = {
let contents = include_str!("style.css");
Expand Down Expand Up @@ -132,6 +139,7 @@ impl AssetPaths {
};

AssetPaths {
root_index,
favicon_png,
spritesheet_png,
style_css,
Expand All @@ -144,6 +152,7 @@ impl AssetPaths {

#[derive(Serialize)]
pub struct AssetPaths {
pub root_index: String,
pub favicon_png: String,
pub spritesheet_png: String,
pub style_css: String,
Expand Down
6 changes: 5 additions & 1 deletion website/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use std::{
pub struct Config {
pub legacy_renderer: bool,
pub mods: Vec<String>,
/// Mod directories are still created in the true root outside of the web root.
/// This is too maintain backwards compatibility with existing links to rukaidata.com
/// We could very easily introduce a web_root_mods field so that mods
/// could be also set to the same directory allowing other users to fully alter the web root.
pub web_root: String,
}

Expand All @@ -20,7 +24,7 @@ impl Config {
} else {
let config = Config {
legacy_renderer: false,
mods: vec![],
mods: vec!["Brawl".to_owned()],
web_root: "/".to_owned(),
};
config.save(&path);
Expand Down
6 changes: 6 additions & 0 deletions website/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ impl OutDir {
OutDir { path }
}

pub fn new_path(path: &Path) -> Self {
let path = Path::new("../root").join(path);
fs::create_dir_all(&path).unwrap();
OutDir { path }
}

pub fn compressed_file_writer(&self, file_name: &str) -> GzEncoder<File> {
let file = File::create(self.path.join(file_name)).unwrap();
GzEncoder::new(file, Compression::best())
Expand Down
3 changes: 2 additions & 1 deletion website/src/page/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub fn generate(handlebars: &Handlebars, brawl_mods: &BrawlMods, assets: &AssetP
assets,
mod_links: brawl_mods.gen_mod_links(String::new()),
};
let file = OutDir::new(".").compressed_file_writer("error.html");
let file =
OutDir::new(assets.root_index.trim_start_matches('/')).compressed_file_writer("error.html");
handlebars.render_to_write("error", &page, file).unwrap();
}

Expand Down
3 changes: 2 additions & 1 deletion website/src/page/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub fn generate(handlebars: &Handlebars, brawl_mods: &BrawlMods, assets: &AssetP
mod_links: brawl_mods.gen_mod_links(String::new()),
assets,
};
let writer = OutDir::new(".").compressed_file_writer("index.html");
let writer =
OutDir::new(assets.root_index.trim_start_matches('/')).compressed_file_writer("index.html");
handlebars.render_to_write("index", &page, writer).unwrap();
}

Expand Down
4 changes: 2 additions & 2 deletions website/templates/base.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="container-fluid">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/">Rukai Data</a>
<a class="nav-link" href="{{assets.root_index}}">Rukai Data</a>
</li>
</ul>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mainNavbar"
Expand Down Expand Up @@ -69,4 +69,4 @@

</body>

</html>
</html>

0 comments on commit 580bf8c

Please sign in to comment.