From bd8d863223fc640b0aef593d0a9322b3f27d9438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Fern=C3=A1ndez=20L=C3=B3pez?= Date: Tue, 3 Oct 2023 09:52:59 +0200 Subject: [PATCH] chore: only serve public directory if it exists --- crates/server/src/lib.rs | 41 +++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index 86d867fa..fed0e0e6 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -64,27 +64,30 @@ pub async fn serve( static_prefix = String::from("/"); } - app = app.service( - Files::new(&static_prefix, root_path.join("public")) - .index_file("index.html") - // This handler check if there's an HTML file in the public folder that - // can reply to the given request. For example, if someone request /about, - // this handler will look for a /public/about.html file. - .default_handler(fn_service(|req: ServiceRequest| async { - let (req, _) = req.into_parts(); + let public_dir = root_path.join("public"); + if public_dir.exists() { + app = app.service( + Files::new(&static_prefix, public_dir) + .index_file("index.html") + // This handler check if there's an HTML file in the public folder that + // can reply to the given request. For example, if someone request /about, + // this handler will look for a /public/about.html file. + .default_handler(fn_service(|req: ServiceRequest| async { + let (req, _) = req.into_parts(); - match handle_assets(&req).await { - Ok(existing_file) => { - let res = existing_file.into_response(&req); - Ok(ServiceResponse::new(req, res)) + match handle_assets(&req).await { + Ok(existing_file) => { + let res = existing_file.into_response(&req); + Ok(ServiceResponse::new(req, res)) + } + Err(_) => { + let res = handle_not_found(&req).await; + Ok(ServiceResponse::new(req, res)) + } } - Err(_) => { - let res = handle_not_found(&req).await; - Ok(ServiceResponse::new(req, res)) - } - } - })), - ); + })), + ); + } app })