Consider introducing default fallback to index file in Files
- Dominant language
- Rust
- Stars
- 24.8k
- Forks
- 1.9k
- Avg merge
- 23h 10m
- Merged PRs (30d)
- 26
Description
## Expected Behavior
I would like to have a function like `default_to_index_file` for the Files service with specified `index_file`. This behavior allows SPA web pages with client-side routing, so it must be pretty popular. Example:
```
App::new()
.service(
Files::new("/", &args.website)
.index_file(&args.index)
.default_to_index_file()
)
.bind(&args.address)?
.run
.await
```
Alternatively, passing a boolean as a second parameter to the `index_file`:
```
App::new()
.service(
Files::new("/", &args.website)
.index_file(&args.index, true)
)
.bind(&args.address)?
.run
.await
```
## Current Behavior
I had to copy-paste someone's little closure to the `default_handler`. Here it is:
```
|req: ServiceRequest| {
let (http_req, _payload) = req.into_parts();
let ctx = http_req.app_data::>().unwrap();
let path = format!("{}/{}", ctx.args.website, ctx.args.index);
async {
let response = NamedFile::open(path)?.into_response(&http_req)?;
Ok(ServiceResponse::new(http_req, response))
}
}
```
Also, since I didn't hardcode the file path — I had to pass my Args (or it could be just that `path`) through the app's data, which I wouldn't do otherwise.
## Context
I have tried to build a simple API + static files server for my coursework's SPA. Newbie in Rust (esp. async) and in general.
Contributor guide
Assessment
This issue has not been assessed yet.