2017-10-07 06:48:14 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2017-12-12 01:26:51 +01:00
|
|
|
use http::{Method, StatusCode};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-12-11 23:16:29 +01:00
|
|
|
use pred;
|
2017-12-12 01:26:51 +01:00
|
|
|
use body::Body;
|
2017-12-05 01:09:22 +01:00
|
|
|
use route::Route;
|
2017-12-12 01:26:51 +01:00
|
|
|
use handler::{Reply, Handler, FromRequest};
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-12-12 01:26:51 +01:00
|
|
|
use httpresponse::HttpResponse;
|
2017-12-04 22:32:05 +01:00
|
|
|
|
2017-12-05 01:09:22 +01:00
|
|
|
/// *Resource* is an entry in route table which corresponds to requested URL.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// Resource in turn has at least one route.
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Route consists of an object that implements `Handler` trait (handler)
|
|
|
|
/// and list of predicates (objects that implement `Predicate` trait).
|
|
|
|
/// Route uses builder-like pattern for configuration.
|
|
|
|
/// During request handling, resource object iterate through all routes
|
|
|
|
/// and check all predicates for specific route, if request matches all predicates route
|
|
|
|
/// route considired matched and route handler get called.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
2017-11-29 22:26:55 +01:00
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2017-12-04 22:32:05 +01:00
|
|
|
/// use actix_web::*;
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-12-11 23:16:29 +01:00
|
|
|
/// let app = Application::new()
|
2017-12-04 22:32:05 +01:00
|
|
|
/// .resource(
|
2017-12-06 20:00:39 +01:00
|
|
|
/// "/", |r| r.method(Method::GET).f(|r| HttpResponse::Ok()))
|
2017-10-15 23:17:41 +02:00
|
|
|
/// .finish();
|
2017-10-08 08:59:57 +02:00
|
|
|
/// }
|
2017-10-08 23:56:51 +02:00
|
|
|
pub struct Resource<S=()> {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String,
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData<S>,
|
2017-12-04 22:32:05 +01:00
|
|
|
routes: Vec<Route<S>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S> Default for Resource<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
fn default() -> Self {
|
2017-10-08 23:56:51 +02:00
|
|
|
Resource {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String::new(),
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData,
|
2017-12-11 23:16:29 +01:00
|
|
|
routes: Vec::new() }
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 01:26:27 +01:00
|
|
|
impl<S> Resource<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-29 14:05:31 +01:00
|
|
|
pub(crate) fn default_not_found() -> Self {
|
|
|
|
Resource {
|
|
|
|
name: String::new(),
|
|
|
|
state: PhantomData,
|
2017-12-11 23:16:29 +01:00
|
|
|
routes: Vec::new() }
|
2017-10-29 14:05:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
/// Set resource name
|
2017-12-03 01:37:21 +01:00
|
|
|
pub fn name<T: Into<String>>(&mut self, name: T) {
|
2017-11-27 02:30:35 +01:00
|
|
|
self.name = name.into();
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
pub(crate) fn get_name(&self) -> &str {
|
|
|
|
&self.name
|
2017-12-05 20:31:35 +01:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Resource<S> {
|
2017-12-05 20:31:35 +01:00
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Register a new route and return mutable reference to *Route* object.
|
|
|
|
/// *Route* is used for route configuration, i.e. adding predicates, setting up handler.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2017-12-04 22:32:05 +01:00
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-12-11 23:16:29 +01:00
|
|
|
/// let app = Application::new()
|
2017-12-04 22:32:05 +01:00
|
|
|
/// .resource(
|
|
|
|
/// "/", |r| r.route()
|
|
|
|
/// .p(pred::Any(vec![pred::Get(), pred::Put()]))
|
|
|
|
/// .p(pred::Header("Content-Type", "text/plain"))
|
2017-12-04 23:07:53 +01:00
|
|
|
/// .f(|r| HttpResponse::Ok()))
|
2017-12-04 22:32:05 +01:00
|
|
|
/// .finish();
|
|
|
|
/// }
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```
|
2017-12-04 22:32:05 +01:00
|
|
|
pub fn route(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap()
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Register a new route and add method check to route.
|
2017-12-06 17:03:08 +01:00
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// Resource::resource("/", |r| r.route().method(Method::GET).f(index)
|
|
|
|
/// ```
|
2017-12-04 22:32:05 +01:00
|
|
|
pub fn method(&mut self, method: Method) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
2017-12-11 23:16:29 +01:00
|
|
|
self.routes.last_mut().unwrap().p(pred::Method(method))
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:03:08 +01:00
|
|
|
/// Register a new route and add handler object.
|
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// Resource::resource("/", |r| r.route().h(handler)
|
|
|
|
/// ```
|
|
|
|
pub fn h<H: Handler<S>>(&mut self, handler: H) {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().h(handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a new route and add handler function.
|
|
|
|
///
|
2017-12-06 20:00:39 +01:00
|
|
|
/// This is shortcut for:
|
|
|
|
///
|
2017-12-06 17:03:08 +01:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// Resource::resource("/", |r| r.route().f(index)
|
|
|
|
/// ```
|
|
|
|
pub fn f<F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
|
|
|
R: FromRequest + 'static,
|
|
|
|
{
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().f(handler)
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-12-12 01:26:51 +01:00
|
|
|
pub(crate) fn handle(&self, mut req: HttpRequest<S>, default: Option<&Resource<S>>)
|
|
|
|
-> Reply
|
|
|
|
{
|
2017-12-04 22:32:05 +01:00
|
|
|
for route in &self.routes {
|
|
|
|
if route.check(&mut req) {
|
|
|
|
return route.handle(req)
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-12-12 01:26:51 +01:00
|
|
|
if let Some(resource) = default {
|
|
|
|
resource.handle(req, None)
|
|
|
|
} else {
|
|
|
|
Reply::response(HttpResponse::new(StatusCode::NOT_FOUND, Body::Empty))
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|