1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

added fs tests

This commit is contained in:
Nikolay Kim
2017-12-03 18:15:09 -08:00
parent 69f0c098e3
commit 5decff9154
4 changed files with 79 additions and 2 deletions

View File

@ -1 +1,41 @@
# Static file handling
## Individual file
It is possible to serve static files with tail path pattern and `NamedFile`.
```rust
extern crate actix_web;
use actix_web::*;
use std::path::PathBuf;
fn index(req: HttpRequest) -> Result<fs::NamedFile> {
let path: PathBuf = req.match_info().query("tail")?;
Ok(fs::NamedFile::open(path)?)
}
fn main() {
Application::default("/")
.resource(r"/a/{tail:*}", |r| r.get(index))
.finish();
}
```
## Directory
To serve all files from specific directory `StaticFiles` type could be used.
`StaticFiles` could be registered with `Application::route` method.
```rust
extern crate actix_web;
fn main() {
actix_web::Application::default("/")
.route("/static", actix_web::fs::StaticFiles::new(".", true))
.finish();
}
```
First parameter is a base directory. Second parameter is `show_index`, if it set to *true*
directory listing would be returned for directories, if it is set to *false*
then *404 Not Found* would be returned instead of directory listing.