From 05511721e74b399033d62204e7c0990adf381be7 Mon Sep 17 00:00:00 2001 From: Giacomo Cariello Date: Sat, 20 May 2023 14:34:41 +0200 Subject: [PATCH 1/2] Added path_prefix_server to Turbine and moved get_path_prefix_server() from free function to Turbine public method. This way, path_prefix_server can be customized in order to build non-standard combinations of multiple Perseus apps. --- packages/perseus/src/init.rs | 6 ++---- packages/perseus/src/turbine/build.rs | 1 + packages/perseus/src/turbine/export.rs | 3 +-- packages/perseus/src/turbine/mod.rs | 22 ++++++++++++++++++++++ packages/perseus/src/turbine/server.rs | 3 +-- packages/perseus/src/utils/path_prefix.rs | 16 ---------------- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/perseus/src/init.rs b/packages/perseus/src/init.rs index ee248ef0fd..cbb9df4e38 100644 --- a/packages/perseus/src/init.rs +++ b/packages/perseus/src/init.rs @@ -1,7 +1,5 @@ #[cfg(engine)] use crate::server::HtmlShell; -#[cfg(engine)] -use crate::utils::get_path_prefix_server; use crate::{ error_views::ErrorViews, i18n::{Locales, TranslationsManager}, @@ -824,10 +822,10 @@ impl PerseusAppBase { root: &str, render_cfg: &HashMap, plugins: &Plugins, + path_prefix_server: &str, ) -> Result { // Construct an HTML shell - let mut html_shell = - HtmlShell::new(index_view_str, root, render_cfg, &get_path_prefix_server()); + let mut html_shell = HtmlShell::new(index_view_str, root, render_cfg, path_prefix_server); // Apply the myriad plugin actions to the HTML shell (replacing the whole thing // first if need be) diff --git a/packages/perseus/src/turbine/build.rs b/packages/perseus/src/turbine/build.rs index 271ca039b0..0017458d69 100644 --- a/packages/perseus/src/turbine/build.rs +++ b/packages/perseus/src/turbine/build.rs @@ -111,6 +111,7 @@ impl Turbine { &self.root_id, &self.render_cfg, &self.plugins, + self.get_path_prefix_server().as_ref(), ) .await?; self.html_shell = Some(html_shell); diff --git a/packages/perseus/src/turbine/export.rs b/packages/perseus/src/turbine/export.rs index b6b81b0606..e8ff0f5c56 100644 --- a/packages/perseus/src/turbine/export.rs +++ b/packages/perseus/src/turbine/export.rs @@ -7,7 +7,6 @@ use crate::{ plugins::PluginAction, state::TemplateState, stores::MutableStore, - utils::get_path_prefix_server, }; use fs_extra::dir::{copy as copy_dir, CopyOptions}; use futures::future::{try_join, try_join_all}; @@ -105,7 +104,7 @@ impl Turbine { // We assume we've already built the app, which would have populated this let html_shell = self.html_shell.as_ref().unwrap(); - let path_prefix = get_path_prefix_server(); + let path_prefix = self.get_path_prefix_server(); // We need the encoded path to reference flattened build artifacts // But we don't create a flattened system with exporting, everything is properly // created in a directory structure diff --git a/packages/perseus/src/turbine/mod.rs b/packages/perseus/src/turbine/mod.rs index 584952f7f7..2b3d8cb30a 100644 --- a/packages/perseus/src/turbine/mod.rs +++ b/packages/perseus/src/turbine/mod.rs @@ -68,6 +68,8 @@ pub struct Turbine { /// The app's global state, kept cached throughout the build process because /// every template we build will need access to it through context. global_state: TemplateState, + /// Custom URL path prefix for perseus. + pub path_prefix_server: Option, /// The HTML shell that can be used for constructing the full pages this app /// returns. html_shell: Option, @@ -114,6 +116,7 @@ impl TryFrom Turbine { &self.root_id, &self.render_cfg, &self.plugins, + self.get_path_prefix_server().as_ref(), ) .await?; self.html_shell = Some(html_shell); Ok(()) } + + /// Gets the path prefix to apply on the server. If `path_prefix_server` is not set + /// on `Turbine`, `PERSEUS_BASE_PATH` environment variable is used to resolve, + /// which avoids hardcoding something as changeable as this into the final binary. + /// Hence however, that variable must be the same as what's set in `` + /// (done automatically). + /// Trailing forward slashes will be trimmed automatically. + #[cfg(engine)] + pub fn get_path_prefix_server(&self) -> String { + self.path_prefix_server.clone().unwrap_or_else(|| { + use std::env; + let base_path = env::var("PERSEUS_BASE_PATH").unwrap_or_else(|_| "".to_string()); + base_path + .strip_suffix('/') + .unwrap_or(&base_path) + .to_string() + }) + } } diff --git a/packages/perseus/src/turbine/server.rs b/packages/perseus/src/turbine/server.rs index 189233656f..df5ff1ca8a 100644 --- a/packages/perseus/src/turbine/server.rs +++ b/packages/perseus/src/turbine/server.rs @@ -8,7 +8,6 @@ use crate::{ server::get_path_slice, state::TemplateState, stores::MutableStore, - utils::get_path_prefix_server, Request, }; use fmterr::fmt_err; @@ -309,7 +308,7 @@ impl Turbine { // bundle will do. &format!( "{}/{}/{}", - get_path_prefix_server(), + self.get_path_prefix_server(), &self.locales.default, // This is a `PathWithoutLocale` redirect_path.0, diff --git a/packages/perseus/src/utils/path_prefix.rs b/packages/perseus/src/utils/path_prefix.rs index 289c702360..1ca7fe4da6 100644 --- a/packages/perseus/src/utils/path_prefix.rs +++ b/packages/perseus/src/utils/path_prefix.rs @@ -1,19 +1,3 @@ -/// Gets the path prefix to apply on the server. This uses the -/// `PERSEUS_BASE_PATH` environment variable, which avoids hardcoding -/// something as changeable as this into the final binary. Hence however, that -/// variable must be the same as what's set in `` (done automatically). -/// Trailing forward slashes will be trimmed automatically. -#[cfg(engine)] -pub fn get_path_prefix_server() -> String { - use std::env; - - let base_path = env::var("PERSEUS_BASE_PATH").unwrap_or_else(|_| "".to_string()); - base_path - .strip_suffix('/') - .unwrap_or(&base_path) - .to_string() -} - /// Gets the path prefix to apply in the browser. This uses the HTML `` /// element, which would be required anyway to make Sycamore's router co-operate /// with a relative path hosting. From b6cfe873f07d2d7567fd7705422e6735e0c45b31 Mon Sep 17 00:00:00 2001 From: Giacomo Cariello Date: Tue, 12 Sep 2023 08:53:12 +0200 Subject: [PATCH 2/2] Better wording for URL prefix description comment. Co-authored-by: Sam Brew --- packages/perseus/src/turbine/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/perseus/src/turbine/mod.rs b/packages/perseus/src/turbine/mod.rs index 2b3d8cb30a..ade1c2f730 100644 --- a/packages/perseus/src/turbine/mod.rs +++ b/packages/perseus/src/turbine/mod.rs @@ -68,7 +68,7 @@ pub struct Turbine { /// The app's global state, kept cached throughout the build process because /// every template we build will need access to it through context. global_state: TemplateState, - /// Custom URL path prefix for perseus. + /// The prefix to place in front of the URL the website will be deployed to. pub path_prefix_server: Option, /// The HTML shell that can be used for constructing the full pages this app /// returns.