From dd16e5db8aa0a7b7c8c23ff6e77dae643f6508a6 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sun, 26 Feb 2023 17:02:09 +0100 Subject: [PATCH] Fix path prefix as / --- src/config.rs | 16 ++++++++++++---- src/main.rs | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3bdb2bf..ab54d09 100644 --- a/src/config.rs +++ b/src/config.rs @@ -141,7 +141,7 @@ impl Default for Strings { #[derive(Deserialize)] pub struct StateConfig { /// The path prefix of all routes. - #[serde(default = "default_path_prefix")] + #[serde(default)] pub path_prefix: String, pub custom_fields: Vec, /// The language tag of the HTML file. @@ -150,9 +150,6 @@ pub struct StateConfig { #[serde(default)] pub strings: Strings, } -fn default_path_prefix() -> String { - "/".to_string() -} fn default_lang() -> String { "en".to_string() } @@ -208,6 +205,17 @@ impl Config { ); }); + // Trim path prefix and set it to empty if it is /. + config.state_config.path_prefix = { + let path_prefix = config.state_config.path_prefix.trim(); + + if path_prefix == "/" { + String::default() + } else { + path_prefix.to_string() + } + }; + Ok(config) } } diff --git a/src/main.rs b/src/main.rs index ad1f164..760e4b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ async fn init(logger_initialized: &mut bool) -> Result<()> { .nest_service("/static", static_service); let app = { - if path_prefix == "/" { + if path_prefix.is_empty() { // No need to nest. routes } else {