actix / actix/actix-web

Consider introducing default fallback to index file in Files

Open
#2,617 2 comments 2 reactions 0 assignees View on GitHub
A-files C-feature
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.