From 542315ce7f94371ca4755fca0ee3f144ac1c8cfc Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 6 Apr 2018 19:34:55 -0700 Subject: [PATCH] simplify StaticFiles --- examples/basics/src/main.rs | 2 +- examples/websocket-chat/src/main.rs | 2 +- examples/websocket/src/main.rs | 2 +- guide/src/qs_12.md | 17 +++++++++++------ src/application.rs | 2 +- src/client/connector.rs | 12 ++++-------- src/fs.rs | 27 ++++++++++++++++----------- 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/examples/basics/src/main.rs b/examples/basics/src/main.rs index cfc9933db..510f02cef 100644 --- a/examples/basics/src/main.rs +++ b/examples/basics/src/main.rs @@ -129,7 +129,7 @@ fn main() { io::Error::new(io::ErrorKind::Other, "test"), StatusCode::OK) })) // static files - .handler("/static/", fs::StaticFiles::new("../static/", true)) + .handler("/static/", fs::StaticFiles::new("../static/")) // redirect .resource("/", |r| r.method(Method::GET).f(|req| { println!("{:?}", req); diff --git a/examples/websocket-chat/src/main.rs b/examples/websocket-chat/src/main.rs index ee5c1c45f..5cd3e6e2c 100644 --- a/examples/websocket-chat/src/main.rs +++ b/examples/websocket-chat/src/main.rs @@ -199,7 +199,7 @@ fn main() { // websocket .resource("/ws/", |r| r.route().f(chat_route)) // static resources - .handler("/static/", fs::StaticFiles::new("static/", true)) + .handler("/static/", fs::StaticFiles::new("static/")) }) .bind("127.0.0.1:8080").unwrap() .start(); diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 07ad7ff46..11292a9be 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -55,7 +55,7 @@ fn main() { // websocket route .resource("/ws/", |r| r.method(http::Method::GET).f(ws_index)) // static files - .handler("/", fs::StaticFiles::new("../static/", true) + .handler("/", fs::StaticFiles::new("../static/") .index_file("index.html"))) // start http server on 127.0.0.1:8080 .bind("127.0.0.1:8080").unwrap() diff --git a/guide/src/qs_12.md b/guide/src/qs_12.md index 2feb5a1be..e399c2eb3 100644 --- a/guide/src/qs_12.md +++ b/guide/src/qs_12.md @@ -34,16 +34,21 @@ use actix_web::*; fn main() { App::new() - .handler("/static", fs::StaticFiles::new(".", true)) + .handler( + "/static", + fs::StaticFiles::new(".") + .show_folder_listing()) .finish(); } ``` -The first parameter is the base directory. If the second parameter, *show_index*, is set to **true**, -the directory listing will be returned, and if it is set to **false**, -*404 Not Found* will be returned. +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/s/struct.StaticFiles.html#method.show_files_listing) +method. -Instead of showing files listing for directory, it is possible to redirect to a specific -index file. Use the +Instead of showing files listing for directory, it is possible to redirect +to a specific index file. Use the [*StaticFiles::index_file()*](../actix_web/s/struct.StaticFiles.html#method.index_file) method to configure this redirect. diff --git a/src/application.rs b/src/application.rs index 872f413eb..9bc51ab7e 100644 --- a/src/application.rs +++ b/src/application.rs @@ -391,7 +391,7 @@ impl App where S: 'static { /// let app = App::new() /// .middleware(middleware::Logger::default()) /// .configure(config) // <- register resources - /// .handler("/static", fs::StaticFiles::new(".", true)); + /// .handler("/static", fs::StaticFiles::new(".")); /// } /// ``` pub fn configure(self, cfg: F) -> App diff --git a/src/client/connector.rs b/src/client/connector.rs index 5e364ae9f..2b9a5e2f1 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -538,8 +538,7 @@ impl ClientConnector { self.install_wait_timeout(wait); let waiter = Waiter{ tx, wait, conn_timeout }; - self.waiters.entry(key.clone()).or_insert_with(VecDeque::new) - .push_back(waiter); + self.waiters.entry(key).or_insert_with(VecDeque::new).push_back(waiter); rx } } @@ -553,10 +552,8 @@ impl Handler for ClientConnector { let mut timeout = Timeout::new(time, Arbiter::handle()).unwrap(); let _ = timeout.poll(); self.paused = Some(Some((when, timeout))); - } else { - if self.paused.is_none() { - self.paused = Some(None); - } + } else if self.paused.is_none() { + self.paused = Some(None); } } } @@ -726,8 +723,7 @@ impl fut::ActorFuture for Maintenance { // check pause duration let done = if let Some(Some(ref pause)) = act.paused { - if pause.0 <= Instant::now() {true} else {false} - } else { false }; + pause.0 <= Instant::now() } else { false }; if done { act.paused.take(); } diff --git a/src/fs.rs b/src/fs.rs index 2d6c0a359..4155aca98 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -372,7 +372,7 @@ impl Responder for Directory { /// /// fn main() { /// let app = App::new() -/// .handler("/static", fs::StaticFiles::new(".", true)) +/// .handler("/static", fs::StaticFiles::new(".")) /// .finish(); /// } /// ``` @@ -388,12 +388,9 @@ pub struct StaticFiles { } impl StaticFiles { - /// Create new `StaticFiles` instance - /// - /// `dir` - base directory - /// - /// `index` - show index for directory - pub fn new>(dir: T, index: bool) -> StaticFiles { + + /// Create new `StaticFiles` instance for specified base directory. + pub fn new>(dir: T) -> StaticFiles { let dir = dir.into(); let (dir, access) = match dir.canonicalize() { @@ -415,7 +412,7 @@ impl StaticFiles { directory: dir, accessible: access, index: None, - show_index: index, + show_index: false, cpu_pool: CpuPool::new(40), default: Box::new(WrapHandler::new( |_| HttpResponse::new(StatusCode::NOT_FOUND))), @@ -424,6 +421,14 @@ impl StaticFiles { } } + /// Show files listing for directories. + /// + /// By default show files listing is disabled. + pub fn show_files_listing(mut self) -> Self { + self.show_index = true; + self + } + /// Set index file /// /// Redirects to specific index file for directory "/" instead of @@ -523,7 +528,7 @@ mod tests { #[test] fn test_static_files() { - let mut st = StaticFiles::new(".", true); + let mut st = StaticFiles::new(".").show_files_listing(); st.accessible = false; let resp = st.handle(HttpRequest::default()).respond_to(HttpRequest::default()).unwrap(); let resp = resp.as_response().expect("HTTP Response"); @@ -548,7 +553,7 @@ mod tests { #[test] fn test_redirect_to_index() { - let mut st = StaticFiles::new(".", false).index_file("index.html"); + let mut st = StaticFiles::new(".").index_file("index.html"); let mut req = HttpRequest::default(); req.match_info_mut().add("tail", "guide"); @@ -568,7 +573,7 @@ mod tests { #[test] fn test_redirect_to_index_nested() { - let mut st = StaticFiles::new(".", false).index_file("Cargo.toml"); + let mut st = StaticFiles::new(".").index_file("Cargo.toml"); let mut req = HttpRequest::default(); req.match_info_mut().add("tail", "examples/basics");