From 769c9af8f94807f809dcdd6962d2382a0fe6783b Mon Sep 17 00:00:00 2001 From: Cameron Dershem Date: Mon, 17 Jun 2019 17:35:47 -0400 Subject: [PATCH] First pass at Static Files. --- content/docs/static-files.md | 104 ++---------------- examples/Cargo.toml | 1 + examples/static-files/Cargo.toml | 10 ++ examples/static-files/src/configuration.rs | 27 +++++ .../static-files/src/configuration_two.rs | 26 +++++ examples/static-files/src/directory.rs | 8 ++ examples/static-files/src/main.rs | 17 +++ 7 files changed, 99 insertions(+), 94 deletions(-) create mode 100644 examples/static-files/Cargo.toml create mode 100644 examples/static-files/src/configuration.rs create mode 100644 examples/static-files/src/configuration_two.rs create mode 100644 examples/static-files/src/directory.rs create mode 100644 examples/static-files/src/main.rs diff --git a/content/docs/static-files.md b/content/docs/static-files.md index 6f3d385..abfc003 100644 --- a/content/docs/static-files.md +++ b/content/docs/static-files.md @@ -9,53 +9,24 @@ weight: 230 It is possible to serve static files with a custom path pattern and `NamedFile`. To match a path tail, we can use a `[.*]` regex. -```rust -extern crate actix_web; -use std::path::PathBuf; -use actix_web::{App, HttpRequest, Result, http::Method, fs::NamedFile}; - -fn index(req: &HttpRequest) -> Result { - let path: PathBuf = req.match_info().query("tail")?; - Ok(NamedFile::open(path)?) -} - -fn main() { - App::new() - .resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index)) - .finish(); -} -``` +{{< include-example example="static-files" file="main.rs" section="individual-file" >}} # Directory -To serve files from specific directories and sub-directories, `StaticFiles` can be used. -`StaticFiles` must be registered with an `App::handler()` method, otherwise +To serve files from specific directories and sub-directories, `Files` can be used. +`Files` must be registered with an `App::service()` method, otherwise it will be unable to serve sub-paths. -```rust -extern crate actix_web; -use actix_web::{App, fs}; +{{< include-example example="static-files" file="directory.rs" section="directory" >}} -fn main() { - App::new() - .handler( - "/static", - fs::StaticFiles::new(".") - .unwrap() - .show_files_listing()) - .finish(); -} -``` - -The parameter is the base directory. By default files listing for sub-directories -is disabled. Attempt to load directory listing will return *404 Not Found* response. -To enable files listing, use -[*StaticFiles::show_files_listing()*](../../actix-web/actix_web/fs/struct.StaticFiles.html#method.show_files_listing) +By default files listing for sub-directories is disabled. Attempt to load directory +listing will return *404 Not Found* response. To enable files listing, use +[*Files::show_files_listing()*](https://docs.rs/actix-files/0.1.2/actix_files/struct.Files.html) method. Instead of showing files listing for directory, it is possible to redirect to a specific index file. Use the -[*StaticFiles::index_file()*](../../actix-web/actix_web/fs/struct.StaticFiles.html#method.index_file) +[*Files::index_file()*](https://docs.rs/actix-files/0.1.2/actix_files/struct.Files.html#method.index_file) method to configure this redirect. # Configuration @@ -71,63 +42,8 @@ for serving files: All of the above methods are optional and provided with the best defaults. But it is possible to customize any of them by implementing the trait onto own struct. -```rust -extern crate mime; -extern crate actix_web; -use actix_web::{App, HttpRequest, Result, http::Method}; -use actix_web::fs::{StaticFileConfig, NamedFile}; -use actix_web::http::header::DispositionType; - -use std::path::PathBuf; - -#[derive(Default)] -struct MyConfig; - -impl StaticFileConfig for MyConfig { - fn content_disposition_map(typ: mime::Name) -> DispositionType { - DispositionType::Attachment - } -} - -fn index(req: &HttpRequest) -> Result> { - let path: PathBuf = req.match_info().query("tail")?; - Ok(NamedFile::open_with_config(path, MyConfig)?) -} - -fn main() { - App::new() - .resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index)) - .finish(); -} -``` +{{< include-example example="static-files" file="configuration.rs" section="config-one" >}} The Configuration cal also be applied to directory service: -```rust -extern crate actix_web; - -use actix_web::{App}; -use actix_web::fs::{StaticFileConfig, StaticFiles}; - -#[derive(Default)] -struct MyConfig; - -impl StaticFileConfig for MyConfig { - fn is_use_etag() -> bool { - false - } - - fn is_use_last_modifier() -> bool { - false - } -} - -fn main() { - App::new() - .handler( - "/static", - StaticFiles::with_config(".", MyConfig).unwrap() - .show_files_listing() - ).finish(); -} -``` +{{< include-example example="static-files" file="configuration_two.rs" section="config-two" >}} diff --git a/examples/Cargo.toml b/examples/Cargo.toml index ae1746f..981e7a6 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -22,4 +22,5 @@ exclude = [ "responses", "testing", "middleware", + "static-files", ] diff --git a/examples/static-files/Cargo.toml b/examples/static-files/Cargo.toml new file mode 100644 index 0000000..242abb0 --- /dev/null +++ b/examples/static-files/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "static-files" +version = "0.1.0" +authors = ["Cameron Dershem "] +edition = "2018" + +[dependencies] +actix-web = "1.0" +actix-files = "0.1" +mime = "*" diff --git a/examples/static-files/src/configuration.rs b/examples/static-files/src/configuration.rs new file mode 100644 index 0000000..b608e0c --- /dev/null +++ b/examples/static-files/src/configuration.rs @@ -0,0 +1,27 @@ +// +// extern crate actix_web; +// extern crate mime; +// use actix_files::{FileConfig, NamedFile}; +// use actix_web::http::header::DispositionType; +// use actix_web::{http::Method, App, HttpRequest, Result}; + +// use std::path::PathBuf; + +// #[derive(Default)] +// struct MyConfig; + +// impl FileConfig for MyConfig { +// fn content_disposition_map(typ: mime::Name) -> DispositionType { +// DispositionType::Attachment +// } +// } + +// fn index(req: &HttpRequest) -> Result> { +// let path: PathBuf = req.match_info().query("tail")?; +// Ok(NamedFile::open_with_config(path, MyConfig)?) +// } + +// fn main() { +// App::new().resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index)); +// } +// diff --git a/examples/static-files/src/configuration_two.rs b/examples/static-files/src/configuration_two.rs new file mode 100644 index 0000000..c9c0850 --- /dev/null +++ b/examples/static-files/src/configuration_two.rs @@ -0,0 +1,26 @@ +// +// use actix_files::{FileConfig, Files}; +// use actix_web::App; + +// #[derive(Default)] +// struct MyConfig; + +// impl FileConfig for MyConfig { +// fn is_use_etag() -> bool { +// false +// } + +// fn is_use_last_modifier() -> bool { +// false +// } +// } + +// fn main() { +// App::new().service( +// "/static", +// Files::with_config(".", MyConfig) +// .unwrap() +// .show_files_listing(), +// ); +// } +// diff --git a/examples/static-files/src/directory.rs b/examples/static-files/src/directory.rs new file mode 100644 index 0000000..b547650 --- /dev/null +++ b/examples/static-files/src/directory.rs @@ -0,0 +1,8 @@ +// +use actix_files as fs; +use actix_web::App; + +fn main() { + App::new().service(fs::Files::new("/static", ".").show_files_listing()); +} +// diff --git a/examples/static-files/src/main.rs b/examples/static-files/src/main.rs new file mode 100644 index 0000000..4d1840a --- /dev/null +++ b/examples/static-files/src/main.rs @@ -0,0 +1,17 @@ +mod configuration; +mod configuration_two; +mod directory; +// +use actix_files::NamedFile; +use actix_web::{web, App, HttpRequest, Result}; +use std::path::PathBuf; + +fn index(req: HttpRequest) -> Result { + let path: PathBuf = req.match_info().query("tail").parse().unwrap(); + Ok(NamedFile::open(path)?) +} + +fn main() { + App::new().route("/", web::get().to(index)); +} +//