2017-10-07 06:48:14 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use actix::Actor;
|
|
|
|
use http::Method;
|
2017-10-15 23:17:41 +02:00
|
|
|
use futures::Stream;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
use task::Task;
|
2017-11-28 21:28:51 +01:00
|
|
|
use error::Error;
|
2017-11-29 18:17:00 +01:00
|
|
|
use route::{Reply, Route, RouteHandler, Frame, FnHandler, StreamHandler};
|
2017-10-07 06:48:14 +02:00
|
|
|
use context::HttpContext;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-29 14:05:31 +01:00
|
|
|
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// Http resource
|
|
|
|
///
|
2017-10-08 23:56:51 +02: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.
|
|
|
|
/// Route corresponds to handling HTTP method by calling route handler.
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
///
|
|
|
|
/// struct MyRoute;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-10-15 23:17:41 +02:00
|
|
|
/// let router = RoutingMap::default()
|
|
|
|
/// .resource("/", |r| r.post::<MyRoute>())
|
|
|
|
/// .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>,
|
|
|
|
routes: HashMap<Method, Box<RouteHandler<S>>>,
|
|
|
|
default: Box<RouteHandler<S>>,
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
routes: HashMap::new(),
|
|
|
|
default: Box::new(HTTPMethodNotAllowed)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S> Resource<S> where S: 'static {
|
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,
|
|
|
|
routes: HashMap::new(),
|
|
|
|
default: Box::new(HTTPNotFound)}
|
|
|
|
}
|
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
/// Set resource name
|
2017-11-27 02:30:35 +01:00
|
|
|
pub fn set_name<T: Into<String>>(&mut self, name: T) {
|
|
|
|
self.name = name.into();
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Register handler for specified method.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
2017-11-28 21:28:51 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-29 18:17:00 +01:00
|
|
|
R: Into<Reply> + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
|
|
|
self.routes.insert(method, Box::new(FnHandler::new(handler)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register async handler for specified method.
|
|
|
|
pub fn async<F, R>(&mut self, method: Method, handler: F)
|
2017-11-27 06:18:38 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-16 07:06:28 +01:00
|
|
|
R: Stream<Item=Frame, Error=Error> + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
|
|
|
self.routes.insert(method, Box::new(StreamHandler::new(handler)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Register handler for specified method.
|
|
|
|
pub fn route_handler<H>(&mut self, method: Method, handler: H)
|
2017-10-07 06:48:14 +02:00
|
|
|
where H: RouteHandler<S>
|
|
|
|
{
|
|
|
|
self.routes.insert(method, Box::new(handler));
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Default handler is used if no matched route found.
|
|
|
|
/// By default `HTTPMethodNotAllowed` is used.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn default_handler<H>(&mut self, handler: H)
|
2017-10-07 06:48:14 +02:00
|
|
|
where H: RouteHandler<S>
|
|
|
|
{
|
|
|
|
self.default = Box::new(handler);
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Handler for `GET` method.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn get<A>(&mut self)
|
2017-10-10 08:07:32 +02:00
|
|
|
where A: Actor<Context=HttpContext<A>> + Route<State=S>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-15 23:17:41 +02:00
|
|
|
self.route_handler(Method::GET, A::factory());
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Handler for `POST` method.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn post<A>(&mut self)
|
2017-10-10 08:07:32 +02:00
|
|
|
where A: Actor<Context=HttpContext<A>> + Route<State=S>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-15 23:17:41 +02:00
|
|
|
self.route_handler(Method::POST, A::factory());
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Handler for `PUR` method.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn put<A>(&mut self)
|
2017-10-10 08:07:32 +02:00
|
|
|
where A: Actor<Context=HttpContext<A>> + Route<State=S>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-15 23:17:41 +02:00
|
|
|
self.route_handler(Method::PUT, A::factory());
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Handler for `METHOD` method.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn delete<A>(&mut self)
|
2017-10-10 08:07:32 +02:00
|
|
|
where A: Actor<Context=HttpContext<A>> + Route<State=S>
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
2017-10-15 23:17:41 +02:00
|
|
|
self.route_handler(Method::DELETE, A::factory());
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S: 'static> RouteHandler<S> for Resource<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-29 04:49:17 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
|
2017-10-07 06:48:14 +02:00
|
|
|
if let Some(handler) = self.routes.get(req.method()) {
|
2017-11-29 04:49:17 +01:00
|
|
|
handler.handle(req, task)
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-11-29 04:49:17 +01:00
|
|
|
self.default.handle(req, task)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|