Skip to content

Commit

Permalink
simplify StaticFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 7, 2018
1 parent 602d78b commit 542315c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/basics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket-chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/websocket/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 11 additions & 6 deletions guide/src/qs_12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<S> App<S> 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<F>(self, cfg: F) -> App<S>
Expand Down
12 changes: 4 additions & 8 deletions src/client/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -553,10 +552,8 @@ impl Handler<Pause> 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);
}
}
}
Expand Down Expand Up @@ -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();
}
Expand Down
27 changes: 16 additions & 11 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
/// }
/// ```
Expand All @@ -388,12 +388,9 @@ pub struct StaticFiles<S> {
}

impl<S: 'static> StaticFiles<S> {
/// Create new `StaticFiles` instance
///
/// `dir` - base directory
///
/// `index` - show index for directory
pub fn new<T: Into<PathBuf>>(dir: T, index: bool) -> StaticFiles<S> {

/// Create new `StaticFiles` instance for specified base directory.
pub fn new<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> {
let dir = dir.into();

let (dir, access) = match dir.canonicalize() {
Expand All @@ -415,7 +412,7 @@ impl<S: 'static> StaticFiles<S> {
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))),
Expand All @@ -424,6 +421,14 @@ impl<S: 'static> StaticFiles<S> {
}
}

/// 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
Expand Down Expand Up @@ -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");
Expand All @@ -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");

Expand All @@ -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");

Expand Down

0 comments on commit 542315c

Please sign in to comment.