Skip to content

Commit 077b7d6

Browse files
ereslibreAngelmmiguel
authored andcommitted
chore: only serve public directory if it exists
1 parent a452150 commit 077b7d6

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

crates/server/src/lib.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,30 @@ pub async fn serve(
7777
static_prefix = String::from("/");
7878
}
7979

80-
app = app.service(
81-
Files::new(&static_prefix, root_path.join("public"))
82-
.index_file("index.html")
83-
// This handler check if there's an HTML file in the public folder that
84-
// can reply to the given request. For example, if someone request /about,
85-
// this handler will look for a /public/about.html file.
86-
.default_handler(fn_service(|req: ServiceRequest| async {
87-
let (req, _) = req.into_parts();
80+
let public_dir = root_path.join("public");
81+
if public_dir.exists() {
82+
app = app.service(
83+
Files::new(&static_prefix, public_dir)
84+
.index_file("index.html")
85+
// This handler check if there's an HTML file in the public folder that
86+
// can reply to the given request. For example, if someone request /about,
87+
// this handler will look for a /public/about.html file.
88+
.default_handler(fn_service(|req: ServiceRequest| async {
89+
let (req, _) = req.into_parts();
8890

89-
match handle_assets(&req).await {
90-
Ok(existing_file) => {
91-
let res = existing_file.into_response(&req);
92-
Ok(ServiceResponse::new(req, res))
91+
match handle_assets(&req).await {
92+
Ok(existing_file) => {
93+
let res = existing_file.into_response(&req);
94+
Ok(ServiceResponse::new(req, res))
95+
}
96+
Err(_) => {
97+
let res = handle_not_found(&req).await;
98+
Ok(ServiceResponse::new(req, res))
99+
}
93100
}
94-
Err(_) => {
95-
let res = handle_not_found(&req).await;
96-
Ok(ServiceResponse::new(req, res))
97-
}
98-
}
99-
})),
100-
);
101+
})),
102+
);
103+
}
101104

102105
app
103106
})

0 commit comments

Comments
 (0)