mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-26 02:19:22 +02:00
simplify StaticFiles
This commit is contained in:
@ -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");
|
||||
|
||||
|
Reference in New Issue
Block a user