mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
simplify StaticFiles
This commit is contained in:
parent
602d78b76c
commit
542315ce7f
@ -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);
|
||||
|
@ -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();
|
||||
|
@ -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()
|
||||
|
@ -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.
|
||||
|
@ -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>
|
||||
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
27
src/fs.rs
27
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<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() {
|
||||
@ -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))),
|
||||
@ -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
|
||||
@ -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");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user