Skip to content

Commit 2534fe2

Browse files
Routing catch-all in path
This update allow you to create a _.sql file that will catch all routing paths in its folder.
1 parent 33f8205 commit 2534fe2

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/webserver/routing.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,17 @@ where
122122
let path_with_ext = path.with_extension(SQL_EXTENSION);
123123
match find_file(&path_with_ext, SQL_EXTENSION, store).await? {
124124
Some(action) => Ok(action),
125-
None => Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH))),
125+
None => {
126+
let mut parent = path.parent();
127+
while let Some(p) = parent {
128+
let target_sql = p.join("_.sql");
129+
if store.contains(&target_sql).await? {
130+
return find_file_or_not_found(&path, SQL_EXTENSION, store).await;
131+
}
132+
parent = p.parent();
133+
}
134+
Ok(Redirect(append_to_path(path_and_query, FORWARD_SLASH)))
135+
}
126136
}
127137
}
128138
}
@@ -136,11 +146,35 @@ where
136146
T: FileStore,
137147
{
138148
match find_file(path, extension, store).await? {
139-
None => find_not_found(path, store).await,
149+
None => find_up_path_tree(path, store).await,
140150
Some(execute) => Ok(execute),
141151
}
142152
}
143153

154+
async fn find_up_path_tree<T>(path: &Path, store: &T) -> anyhow::Result<RoutingAction>
155+
where
156+
T: FileStore,
157+
{
158+
let mut parent = path.parent();
159+
debug!("Starting search for 404 or index file from: {}", path.display());
160+
while let Some(p) = parent {
161+
debug!("Checking parent path: {}", p.display());
162+
let target_404 = p.join(NOT_FOUND);
163+
if store.contains(&target_404).await? {
164+
return Ok(CustomNotFound(target_404));
165+
}
166+
167+
let target_sql = p.join("_.sql");
168+
if store.contains(&target_sql).await? {
169+
return Ok(Execute(target_sql));
170+
}
171+
172+
parent = p.parent();
173+
}
174+
175+
Ok(NotFound)
176+
}
177+
144178
async fn find_file<T>(
145179
path: &Path,
146180
extension: &str,
@@ -160,22 +194,6 @@ where
160194
}
161195
}
162196

163-
async fn find_not_found<T>(path: &Path, store: &T) -> anyhow::Result<RoutingAction>
164-
where
165-
T: FileStore,
166-
{
167-
let mut parent = path.parent();
168-
while let Some(p) = parent {
169-
let target = p.join(NOT_FOUND);
170-
if store.contains(&target).await? {
171-
return Ok(CustomNotFound(target));
172-
}
173-
parent = p.parent();
174-
}
175-
176-
Ok(NotFound)
177-
}
178-
179197
fn append_to_path(path_and_query: &PathAndQuery, append: &str) -> String {
180198
let mut full_uri = path_and_query.to_string();
181199
full_uri.insert_str(path_and_query.path().len(), append);

0 commit comments

Comments
 (0)