Skip to content

Commit

Permalink
chore: only serve public directory if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ereslibre authored and Angelmmiguel committed Oct 6, 2023
1 parent c8299d7 commit bd8d863
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down

0 comments on commit bd8d863

Please sign in to comment.