actix / actix/actix-web

Consider introducing default fallback to index file in Files

Aperta
#2,617 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub
A-files C-feature
Lingua principale
Rust
Stelle
24.8k
Fork
1.9k
Merge medio
23h 10m
PR unite (30g)
26

Descrizione

## 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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.