Skip to content

Commit bd8d863

Browse files
ereslibreAngelmmiguel
authored andcommitted
chore: only serve public directory if it exists
1 parent c8299d7 commit bd8d863

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
@@ -64,27 +64,30 @@ pub async fn serve(
6464
static_prefix = String::from("/");
6565
}
6666

67-
app = app.service(
68-
Files::new(&static_prefix, root_path.join("public"))
69-
.index_file("index.html")
70-
// This handler check if there's an HTML file in the public folder that
71-
// can reply to the given request. For example, if someone request /about,
72-
// this handler will look for a /public/about.html file.
73-
.default_handler(fn_service(|req: ServiceRequest| async {
74-
let (req, _) = req.into_parts();
67+
let public_dir = root_path.join("public");
68+
if public_dir.exists() {
69+
app = app.service(
70+
Files::new(&static_prefix, public_dir)
71+
.index_file("index.html")
72+
// This handler check if there's an HTML file in the public folder that
73+
// can reply to the given request. For example, if someone request /about,
74+
// this handler will look for a /public/about.html file.
75+
.default_handler(fn_service(|req: ServiceRequest| async {
76+
let (req, _) = req.into_parts();
7577

76-
match handle_assets(&req).await {
77-
Ok(existing_file) => {
78-
let res = existing_file.into_response(&req);
79-
Ok(ServiceResponse::new(req, res))
78+
match handle_assets(&req).await {
79+
Ok(existing_file) => {
80+
let res = existing_file.into_response(&req);
81+
Ok(ServiceResponse::new(req, res))
82+
}
83+
Err(_) => {
84+
let res = handle_not_found(&req).await;
85+
Ok(ServiceResponse::new(req, res))
86+
}
8087
}
81-
Err(_) => {
82-
let res = handle_not_found(&req).await;
83-
Ok(ServiceResponse::new(req, res))
84-
}
85-
}
86-
})),
87-
);
88+
})),
89+
);
90+
}
8891

8992
app
9093
})

0 commit comments

Comments
 (0)