Skip to content

Commit 357dd06

Browse files
committed
fix: strip ending / from routes + rename map_dir
1 parent 5240fb3 commit 357dd06

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/router.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl FileServer {
2323
}
2424
}
2525

26-
pub fn add_dir_mount(mut self, route: &str, dir_path: &str) -> Result<Self> {
26+
pub fn map_dir(mut self, route: &str, dir_path: &str) -> Result<Self> {
2727
let mount_point = MountPoint {
2828
route: route.to_owned(),
2929
fs_path: PathBuf::from(dir_path),
@@ -38,8 +38,10 @@ impl FileServer {
3838
}
3939

4040
pub fn map_file(mut self, route: &str, file_path: &str) -> Result<Self> {
41+
let route = route.strip_suffix('/').unwrap_or(route).to_owned();
42+
4143
let mount_point = MountPoint {
42-
route: route.to_owned(),
44+
route,
4345
fs_path: PathBuf::from(file_path),
4446
is_directory: false,
4547
};
@@ -136,24 +138,24 @@ impl Router {
136138
let route = Route::from_str(&route_def)?;
137139
trace!("trying to match route: {route_def}");
138140

139-
if let Some(file_server) = &self.file_server {
140-
match file_server.handle_static_file_access(&route.path) {
141-
Ok(file_path) => {
142-
let mime_type = mime_guess::from_path(&file_path).first_or_octet_stream();
143-
let content = fs::read(file_path)?;
144-
145-
return HttpResponseBuilder::new()
146-
.set_raw_body(content)
147-
.set_content_type(mime_type.as_ref())
148-
.build();
149-
}
150-
Err(e) => warn!("failed to match file: {e}"),
151-
}
152-
}
153-
154141
let response = if let Some(route_callback) = self.routes.get(&route) {
155142
route_callback(request)
156143
} else {
144+
if let Some(file_server) = &self.file_server {
145+
match file_server.handle_static_file_access(&route.path) {
146+
Ok(file_path) => {
147+
let mime_type = mime_guess::from_path(&file_path).first_or_octet_stream();
148+
let content = fs::read(file_path)?;
149+
150+
return HttpResponseBuilder::new()
151+
.set_raw_body(content)
152+
.set_content_type(mime_type.as_ref())
153+
.build();
154+
}
155+
Err(e) => warn!("failed to match file: {e}"),
156+
}
157+
}
158+
157159
let catch_all_route = Route::from_str("GET /*")?;
158160
if let Some(catch_all_callback) = self.routes.get(&catch_all_route) {
159161
return catch_all_callback(request);
@@ -249,16 +251,11 @@ impl FromStr for Route {
249251
type Err = anyhow::Error;
250252

251253
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
252-
let (verb, path) = s.split_once(" ").context("route should have: VERB PATH")?;
253-
let verb = HttpMethod::from_str(verb)?;
254-
255-
let path = if path.ends_with('/') {
256-
path.to_owned()
257-
} else {
258-
format!("{}/", path)
259-
};
254+
let (method, path) = s.split_once(" ").context("route should have: VERB PATH")?;
255+
let method = HttpMethod::from_str(method)?;
256+
let path = path.strip_suffix('/').unwrap_or(path).to_owned();
260257

261-
Ok(Route { method: verb, path })
258+
Ok(Route { method, path })
262259
}
263260
}
264261

0 commit comments

Comments
 (0)