From 2534fe2795728630cd7807ffe1a2cfcca52b551c Mon Sep 17 00:00:00 2001 From: Andrews Innovations Date: Tue, 17 Jun 2025 23:04:52 -0500 Subject: [PATCH 1/5] Routing catch-all in path This update allow you to create a _.sql file that will catch all routing paths in its folder. --- src/webserver/routing.rs | 54 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/webserver/routing.rs b/src/webserver/routing.rs index 4fce3c05..cae6ddfc 100644 --- a/src/webserver/routing.rs +++ b/src/webserver/routing.rs @@ -122,7 +122,17 @@ where let path_with_ext = path.with_extension(SQL_EXTENSION); match find_file(&path_with_ext, SQL_EXTENSION, store).await? { Some(action) => Ok(action), - None => Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH))), + None => { + let mut parent = path.parent(); + while let Some(p) = parent { + let target_sql = p.join("_.sql"); + if store.contains(&target_sql).await? { + return find_file_or_not_found(&path, SQL_EXTENSION, store).await; + } + parent = p.parent(); + } + Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH))) + } } } } @@ -136,11 +146,35 @@ where T: FileStore, { match find_file(path, extension, store).await? { - None => find_not_found(path, store).await, + None => find_up_path_tree(path, store).await, Some(execute) => Ok(execute), } } +async fn find_up_path_tree(path: &Path, store: &T) -> anyhow::Result +where + T: FileStore, +{ + let mut parent = path.parent(); + debug!("Starting search for 404 or index file from: {}", path.display()); + while let Some(p) = parent { + debug!("Checking parent path: {}", p.display()); + let target_404 = p.join(NOT_FOUND); + if store.contains(&target_404).await? { + return Ok(CustomNotFound(target_404)); + } + + let target_sql = p.join("_.sql"); + if store.contains(&target_sql).await? { + return Ok(Execute(target_sql)); + } + + parent = p.parent(); + } + + Ok(NotFound) +} + async fn find_file( path: &Path, extension: &str, @@ -160,22 +194,6 @@ where } } -async fn find_not_found(path: &Path, store: &T) -> anyhow::Result -where - T: FileStore, -{ - let mut parent = path.parent(); - while let Some(p) = parent { - let target = p.join(NOT_FOUND); - if store.contains(&target).await? { - return Ok(CustomNotFound(target)); - } - parent = p.parent(); - } - - Ok(NotFound) -} - fn append_to_path(path_and_query: &PathAndQuery, append: &str) -> String { let mut full_uri = path_and_query.to_string(); full_uri.insert_str(path_and_query.path().len(), append); From a24b69bfbbd3ae7e4b683a8b839d267532f20fa6 Mon Sep 17 00:00:00 2001 From: Andrews Innovations Date: Tue, 17 Jun 2025 23:25:49 -0500 Subject: [PATCH 2/5] Added ability to add catchall to root --- src/webserver/routing.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/webserver/routing.rs b/src/webserver/routing.rs index cae6ddfc..0806b858 100644 --- a/src/webserver/routing.rs +++ b/src/webserver/routing.rs @@ -117,7 +117,17 @@ where { if path_and_query.path().ends_with(FORWARD_SLASH) { path.push(INDEX); - find_file_or_not_found(&path, SQL_EXTENSION, store).await + match find_file_or_not_found(&path, SQL_EXTENSION, store).await { + Ok(NotFound) => { + let target_sql = path.parent().unwrap_or_else(|| Path::new("/")).join("_.sql"); + if store.contains(&target_sql).await? { + Ok(Execute(target_sql)) + } else { + Ok(NotFound) + } + } + result => result, + } } else { let path_with_ext = path.with_extension(SQL_EXTENSION); match find_file(&path_with_ext, SQL_EXTENSION, store).await? { @@ -156,9 +166,7 @@ where T: FileStore, { let mut parent = path.parent(); - debug!("Starting search for 404 or index file from: {}", path.display()); while let Some(p) = parent { - debug!("Checking parent path: {}", p.display()); let target_404 = p.join(NOT_FOUND); if store.contains(&target_404).await? { return Ok(CustomNotFound(target_404)); From cb9aaccabe6a6549da5fb9585e5a99add77b23dc Mon Sep 17 00:00:00 2001 From: Andrews Innovations Date: Tue, 17 Jun 2025 23:40:50 -0500 Subject: [PATCH 3/5] Updated formatting with cargo fmt --- src/webserver/routing.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/webserver/routing.rs b/src/webserver/routing.rs index 0806b858..27638e4c 100644 --- a/src/webserver/routing.rs +++ b/src/webserver/routing.rs @@ -119,7 +119,10 @@ where path.push(INDEX); match find_file_or_not_found(&path, SQL_EXTENSION, store).await { Ok(NotFound) => { - let target_sql = path.parent().unwrap_or_else(|| Path::new("/")).join("_.sql"); + let target_sql = path + .parent() + .unwrap_or_else(|| Path::new("/")) + .join("_.sql"); if store.contains(&target_sql).await? { Ok(Execute(target_sql)) } else { From 35168ac5ca7adcb30faf771bb49534d54bd06bc0 Mon Sep 17 00:00:00 2001 From: Andrews Innovations Date: Tue, 17 Jun 2025 23:53:39 -0500 Subject: [PATCH 4/5] Fix for clippy errors Clippy returned errors for routing, and also an unrelated block of code with a redundant else block. --- src/webserver/http.rs | 37 ++++++++++++++++++------------------ src/webserver/routing.rs | 41 ++++++++++++++++++---------------------- test/models/_.sql | 2 ++ 3 files changed, 39 insertions(+), 41 deletions(-) create mode 100644 test/models/_.sql diff --git a/src/webserver/http.rs b/src/webserver/http.rs index ac0bc1e6..63ccdc25 100644 --- a/src/webserver/http.rs +++ b/src/webserver/http.rs @@ -533,24 +533,25 @@ pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<( } #[cfg(not(target_family = "unix"))] anyhow::bail!("Unix sockets are not supported on your operating system. Use listen_on instead of unix_socket."); - } else { - if let Some(domain) = &config.https_domain { - let mut listen_on_https = listen_on; - listen_on_https.set_port(443); - log::debug!("Will start HTTPS server on {listen_on_https}"); - let config = make_auto_rustls_config(domain, config); - server = server - .bind_rustls_0_23(listen_on_https, config) - .map_err(|e| bind_error(e, listen_on_https))?; - } else if listen_on.port() == 443 { - bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS (port 443)"); - } - if listen_on.port() != 443 { - log::debug!("Will start HTTP server on {listen_on}"); - server = server - .bind(listen_on) - .map_err(|e| bind_error(e, listen_on))?; - } + } + + if let Some(domain) = &config.https_domain { + let mut listen_on_https = listen_on; + listen_on_https.set_port(443); + log::debug!("Will start HTTPS server on {listen_on_https}"); + let config = make_auto_rustls_config(domain, config); + server = server + .bind_rustls_0_23(listen_on_https, config) + .map_err(|e| bind_error(e, listen_on_https))?; + } else if listen_on.port() == 443 { + bail!("Please specify a value for https_domain in the configuration file. This is required when using HTTPS (port 443)"); + } + + if listen_on.port() != 443 { + log::debug!("Will start HTTP server on {listen_on}"); + server = server + .bind(listen_on) + .map_err(|e| bind_error(e, listen_on))?; } log_welcome_message(config); diff --git a/src/webserver/routing.rs b/src/webserver/routing.rs index 27638e4c..87b126f5 100644 --- a/src/webserver/routing.rs +++ b/src/webserver/routing.rs @@ -117,35 +117,30 @@ where { if path_and_query.path().ends_with(FORWARD_SLASH) { path.push(INDEX); - match find_file_or_not_found(&path, SQL_EXTENSION, store).await { - Ok(NotFound) => { - let target_sql = path - .parent() - .unwrap_or_else(|| Path::new("/")) - .join("_.sql"); - if store.contains(&target_sql).await? { - Ok(Execute(target_sql)) - } else { - Ok(NotFound) - } + if let Ok(NotFound) = find_file_or_not_found(&path, SQL_EXTENSION, store).await { + let target_sql = path.parent().unwrap_or_else(|| Path::new("/")).join("_.sql"); + if store.contains(&target_sql).await? { + Ok(Execute(target_sql)) + } else { + Ok(NotFound) } - result => result, + } else { + find_file_or_not_found(&path, SQL_EXTENSION, store).await } } else { let path_with_ext = path.with_extension(SQL_EXTENSION); - match find_file(&path_with_ext, SQL_EXTENSION, store).await? { - Some(action) => Ok(action), - None => { - let mut parent = path.parent(); - while let Some(p) = parent { - let target_sql = p.join("_.sql"); - if store.contains(&target_sql).await? { - return find_file_or_not_found(&path, SQL_EXTENSION, store).await; - } - parent = p.parent(); + if let Some(action) = find_file(&path_with_ext, SQL_EXTENSION, store).await? { + Ok(action) + } else { + let mut parent = path.parent(); + while let Some(p) = parent { + let target_sql = p.join("_.sql"); + if store.contains(&target_sql).await? { + return find_file_or_not_found(&path, SQL_EXTENSION, store).await; } - Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH))) + parent = p.parent(); } + Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH))) } } } diff --git a/test/models/_.sql b/test/models/_.sql new file mode 100644 index 00000000..a0b1b95a --- /dev/null +++ b/test/models/_.sql @@ -0,0 +1,2 @@ +select sqlpage.path(); +select sqlpage.variables(); \ No newline at end of file From cd98bc69ff49a8ef2f3c4f5c0455e957edade0bd Mon Sep 17 00:00:00 2001 From: Andrews Innovations Date: Tue, 17 Jun 2025 23:56:22 -0500 Subject: [PATCH 5/5] Cargo reformat, removed test _.sql file --- src/webserver/routing.rs | 5 ++++- test/models/_.sql | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) delete mode 100644 test/models/_.sql diff --git a/src/webserver/routing.rs b/src/webserver/routing.rs index 87b126f5..125cce8d 100644 --- a/src/webserver/routing.rs +++ b/src/webserver/routing.rs @@ -118,7 +118,10 @@ where if path_and_query.path().ends_with(FORWARD_SLASH) { path.push(INDEX); if let Ok(NotFound) = find_file_or_not_found(&path, SQL_EXTENSION, store).await { - let target_sql = path.parent().unwrap_or_else(|| Path::new("/")).join("_.sql"); + let target_sql = path + .parent() + .unwrap_or_else(|| Path::new("/")) + .join("_.sql"); if store.contains(&target_sql).await? { Ok(Execute(target_sql)) } else { diff --git a/test/models/_.sql b/test/models/_.sql deleted file mode 100644 index a0b1b95a..00000000 --- a/test/models/_.sql +++ /dev/null @@ -1,2 +0,0 @@ -select sqlpage.path(); -select sqlpage.variables(); \ No newline at end of file