mirror of
https://github.com/actix/actix-web.git
synced 2024-12-23 00:26:34 +00:00
Add route scopes #202
This commit is contained in:
parent
aa757a5be8
commit
368730f5f1
5 changed files with 53 additions and 10 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
## 0.6.0 (...)
|
||||
|
||||
* Add route scopes #202
|
||||
|
||||
* Websocket CloseCode Empty/Status is ambiguous #193
|
||||
|
||||
* Add Content-Disposition to NamedFile #204
|
||||
|
|
|
@ -2,8 +2,7 @@ use std::cell::UnsafeCell;
|
|||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
use handler::Reply;
|
||||
use handler::{FromRequest, Handler, Responder, RouteHandler, WrapHandler};
|
||||
use handler::{FromRequest, Handler, Reply, Responder, RouteHandler, WrapHandler};
|
||||
use header::ContentEncoding;
|
||||
use http::Method;
|
||||
use httprequest::HttpRequest;
|
||||
|
@ -11,6 +10,7 @@ use middleware::Middleware;
|
|||
use pipeline::{HandlerType, Pipeline, PipelineHandler};
|
||||
use resource::ResourceHandler;
|
||||
use router::{Resource, Router};
|
||||
use scope::Scope;
|
||||
use server::{HttpHandler, HttpHandlerTask, IntoHttpHandler, ServerSettings};
|
||||
|
||||
#[deprecated(since = "0.5.0", note = "please use `actix_web::App` instead")]
|
||||
|
@ -76,9 +76,9 @@ impl<S: 'static> HttpApplication<S> {
|
|||
&*(&req.path()[inner.prefix + prefix.len()..] as *const _)
|
||||
};
|
||||
if path.is_empty() {
|
||||
req.match_info_mut().add("tail", "");
|
||||
req.match_info_mut().add("tail", "/");
|
||||
} else {
|
||||
req.match_info_mut().add("tail", path.split_at(1).1);
|
||||
req.match_info_mut().add("tail", path);
|
||||
}
|
||||
return HandlerType::Handler(idx);
|
||||
}
|
||||
|
@ -300,6 +300,48 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Configure scope for common root path.
|
||||
///
|
||||
/// Scopes collect multiple paths under a common path prefix.
|
||||
/// Scope path can not contain variable path segments as resources.
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new()
|
||||
/// .scope("/app", |scope| {
|
||||
/// scope.resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok()))
|
||||
/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// In the above example three routes get registered:
|
||||
/// * /app/path1 - reponds to all http method
|
||||
/// * /app/path2 - `GET` requests
|
||||
/// * /app/path3 - `HEAD` requests
|
||||
///
|
||||
pub fn scope<F>(mut self, path: &str, f: F) -> App<S>
|
||||
where
|
||||
F: FnOnce(Scope<S>) -> Scope<S>,
|
||||
{
|
||||
{
|
||||
let scope = Box::new(f(Scope::new()));
|
||||
|
||||
let mut path = path.trim().trim_right_matches('/').to_owned();
|
||||
if !path.is_empty() && !path.starts_with('/') {
|
||||
path.insert(0, '/')
|
||||
}
|
||||
let parts = self.parts.as_mut().expect("Use after finish");
|
||||
|
||||
parts.handlers.push((path, scope));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Configure resource for a specific path.
|
||||
///
|
||||
/// Resources may have variable path segments. For example, a
|
||||
|
|
|
@ -149,6 +149,7 @@ mod pipeline;
|
|||
mod resource;
|
||||
mod route;
|
||||
mod router;
|
||||
mod scope;
|
||||
mod uri;
|
||||
mod with;
|
||||
|
||||
|
@ -171,6 +172,7 @@ pub use httpmessage::HttpMessage;
|
|||
pub use httprequest::HttpRequest;
|
||||
pub use httpresponse::HttpResponse;
|
||||
pub use json::Json;
|
||||
pub use scope::Scope;
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod httpcodes;
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use futures::{Async, Future, Poll};
|
||||
use std::cell::UnsafeCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use error::Error;
|
||||
use handler::{AsyncHandler, FromRequest, Handler, Reply, ReplyItem, Responder,
|
||||
RouteHandler, WrapHandler};
|
||||
|
|
|
@ -388,11 +388,7 @@ where
|
|||
}
|
||||
}
|
||||
Err(e) => {
|
||||
return if e.kind() == io::ErrorKind::WouldBlock {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
};
|
||||
return e.kind() == io::ErrorKind::WouldBlock;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue