Skip to content

Commit f1f68c3

Browse files
ereslibreAngelmmiguel
authored andcommitted
chore: only serve public directory if it exists
1 parent 64e55c1 commit f1f68c3

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
@@ -86,27 +86,30 @@ pub async fn serve(
8686
static_prefix = String::from("/");
8787
}
8888

89-
app = app.service(
90-
Files::new(&static_prefix, root_path.join("public"))
91-
.index_file("index.html")
92-
// This handler check if there's an HTML file in the public folder that
93-
// can reply to the given request. For example, if someone request /about,
94-
// this handler will look for a /public/about.html file.
95-
.default_handler(fn_service(|req: ServiceRequest| async {
96-
let (req, _) = req.into_parts();
89+
let public_dir = root_path.join("public");
90+
if public_dir.exists() {
91+
app = app.service(
92+
Files::new(&static_prefix, public_dir)
93+
.index_file("index.html")
94+
// This handler check if there's an HTML file in the public folder that
95+
// can reply to the given request. For example, if someone request /about,
96+
// this handler will look for a /public/about.html file.
97+
.default_handler(fn_service(|req: ServiceRequest| async {
98+
let (req, _) = req.into_parts();
9799

98-
match handle_assets(&req).await {
99-
Ok(existing_file) => {
100-
let res = existing_file.into_response(&req);
101-
Ok(ServiceResponse::new(req, res))
100+
match handle_assets(&req).await {
101+
Ok(existing_file) => {
102+
let res = existing_file.into_response(&req);
103+
Ok(ServiceResponse::new(req, res))
104+
}
105+
Err(_) => {
106+
let res = handle_not_found(&req).await;
107+
Ok(ServiceResponse::new(req, res))
108+
}
102109
}
103-
Err(_) => {
104-
let res = handle_not_found(&req).await;
105-
Ok(ServiceResponse::new(req, res))
106-
}
107-
}
108-
})),
109-
);
110+
})),
111+
);
112+
}
110113

111114
app
112115
})

0 commit comments

Comments
 (0)