Skip to content

Commit 9d4d09f

Browse files
committed
Fix a crash on startup when a listener has an empty prefix
1 parent c39dcd6 commit 9d4d09f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

crates/cli/src/server.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,15 @@ pub fn build_router(
238238
}
239239
}
240240

241-
if let Some(prefix) = prefix {
242-
let path = format!("{}/", prefix.trim_end_matches('/'));
243-
router = Router::new().nest(&path, router);
241+
// We normalize the prefix:
242+
// - if it's None, it becomes '/'
243+
// - if it's Some(..), any trailing '/' is first trimmed, then a '/' is added
244+
let prefix = format!("{}/", prefix.unwrap_or_default().trim_end_matches('/'));
245+
// Then we only nest the router if the prefix is not empty and not the root
246+
// If we blindly nest the router if the prefix is Some("/"), axum will panic as
247+
// we're not supposed to nest the router at the root
248+
if !prefix.is_empty() && prefix != "/" {
249+
router = Router::new().nest(&prefix, router);
244250
}
245251

246252
router = router.fallback(mas_handlers::fallback);

0 commit comments

Comments
 (0)