mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 10:42:55 +01:00
allow to register handlers on scope level #465
This commit is contained in:
parent
6464f96f8b
commit
f2f05e7715
@ -6,6 +6,9 @@
|
|||||||
|
|
||||||
* Added the ability to pass a custom `TlsConnector`.
|
* Added the ability to pass a custom `TlsConnector`.
|
||||||
|
|
||||||
|
* Allow to register handlers on scope level #465
|
||||||
|
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
* Handle socket read disconnect
|
* Handle socket read disconnect
|
||||||
|
71
src/scope.rs
71
src/scope.rs
@ -5,7 +5,10 @@ use std::rc::Rc;
|
|||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use handler::{AsyncResult, AsyncResultItem, FromRequest, Responder, RouteHandler};
|
use handler::{
|
||||||
|
AsyncResult, AsyncResultItem, FromRequest, Handler, Responder, RouteHandler,
|
||||||
|
WrapHandler,
|
||||||
|
};
|
||||||
use http::Method;
|
use http::Method;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
@ -286,6 +289,44 @@ impl<S: 'static> Scope<S> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure handler for specific path prefix.
|
||||||
|
///
|
||||||
|
/// A path prefix consists of valid path segments, i.e for the
|
||||||
|
/// prefix `/app` any request with the paths `/app`, `/app/` or
|
||||||
|
/// `/app/test` would match, but the path `/application` would
|
||||||
|
/// not.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # extern crate actix_web;
|
||||||
|
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let app = App::new().scope("/scope-prefix", |scope| {
|
||||||
|
/// handler("/app", |req: &HttpRequest| match *req.method() {
|
||||||
|
/// http::Method::GET => HttpResponse::Ok(),
|
||||||
|
/// http::Method::POST => HttpResponse::MethodNotAllowed(),
|
||||||
|
/// _ => HttpResponse::NotFound(),
|
||||||
|
/// })
|
||||||
|
/// });
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> Scope<S> {
|
||||||
|
{
|
||||||
|
let mut path = path.trim().trim_right_matches('/').to_owned();
|
||||||
|
if !path.is_empty() && !path.starts_with('/') {
|
||||||
|
path.insert(0, '/')
|
||||||
|
}
|
||||||
|
if path.len() > 1 && path.ends_with('/') {
|
||||||
|
path.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
Rc::get_mut(&mut self.router)
|
||||||
|
.expect("Multiple copies of scope router")
|
||||||
|
.register_handler(&path, Box::new(WrapHandler::new(handler)), None);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Register a scope middleware
|
/// Register a scope middleware
|
||||||
///
|
///
|
||||||
/// This is similar to `App's` middlewares, but
|
/// This is similar to `App's` middlewares, but
|
||||||
@ -1120,4 +1161,32 @@ mod tests {
|
|||||||
let resp = app.run(req);
|
let resp = app.run(req);
|
||||||
assert_eq!(resp.as_msg().status(), StatusCode::METHOD_NOT_ALLOWED);
|
assert_eq!(resp.as_msg().status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_handler() {
|
||||||
|
let app = App::new()
|
||||||
|
.scope("/scope", |scope| {
|
||||||
|
scope.handler("/test", |_: &_| HttpResponse::Ok())
|
||||||
|
}).finish();
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/scope/test").request();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/scope/test/").request();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/scope/test/app").request();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/scope/testapp").request();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/scope/blah").request();
|
||||||
|
let resp = app.run(req);
|
||||||
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user