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 db4c8bd commit 7fcae8e
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions crates/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,30 @@ pub async fn serve(serve_options: ServeOptions) -> Result<Server> {
static_prefix = String::from("/");
}

app = app.service(
Files::new(&static_prefix, app_data.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();

match handle_assets(&req).await {
Ok(existing_file) => {
let res = existing_file.into_response(&req);
Ok(ServiceResponse::new(req, res))
let public_dir = app_data.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))
}
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 7fcae8e

Please sign in to comment.