Skip to content

Commit 7fcae8e

Browse files
ereslibreAngelmmiguel
authored andcommitted
chore: only serve public directory if it exists
1 parent db4c8bd commit 7fcae8e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

crates/server/src/lib.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,30 @@ pub async fn serve(serve_options: ServeOptions) -> Result<Server> {
129129
static_prefix = String::from("/");
130130
}
131131

132-
app = app.service(
133-
Files::new(&static_prefix, app_data.root_path.join("public"))
134-
.index_file("index.html")
135-
// This handler check if there's an HTML file in the public folder that
136-
// can reply to the given request. For example, if someone request /about,
137-
// this handler will look for a /public/about.html file.
138-
.default_handler(fn_service(|req: ServiceRequest| async {
139-
let (req, _) = req.into_parts();
140-
141-
match handle_assets(&req).await {
142-
Ok(existing_file) => {
143-
let res = existing_file.into_response(&req);
144-
Ok(ServiceResponse::new(req, res))
132+
let public_dir = app_data.root_path.join("public");
133+
if public_dir.exists() {
134+
app = app.service(
135+
Files::new(&static_prefix, public_dir)
136+
.index_file("index.html")
137+
// This handler check if there's an HTML file in the public folder that
138+
// can reply to the given request. For example, if someone request /about,
139+
// this handler will look for a /public/about.html file.
140+
.default_handler(fn_service(|req: ServiceRequest| async {
141+
let (req, _) = req.into_parts();
142+
143+
match handle_assets(&req).await {
144+
Ok(existing_file) => {
145+
let res = existing_file.into_response(&req);
146+
Ok(ServiceResponse::new(req, res))
147+
}
148+
Err(_) => {
149+
let res = handle_not_found(&req).await;
150+
Ok(ServiceResponse::new(req, res))
151+
}
145152
}
146-
Err(_) => {
147-
let res = handle_not_found(&req).await;
148-
Ok(ServiceResponse::new(req, res))
149-
}
150-
}
151-
})),
152-
);
153+
})),
154+
);
155+
}
153156

154157
app
155158
})

0 commit comments

Comments
 (0)