2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-13 23:43:17 +02:00
|
|
|
use std::convert::From;
|
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-10-15 23:17:41 +02:00
|
|
|
use route::{Route, RouteHandler, Frame, FnHandler, StreamHandler};
|
2017-10-09 05:16:48 +02:00
|
|
|
use payload::Payload;
|
2017-10-07 06:48:14 +02:00
|
|
|
use context::HttpContext;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-15 18:33:17 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2017-10-07 06:48:14 +02:00
|
|
|
use httpcodes::HTTPMethodNotAllowed;
|
|
|
|
|
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-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-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-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)
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Into<HttpResponse> + 'static,
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
|
|
|
|
R: Stream<Item=Frame, Error=()> + 'static,
|
|
|
|
{
|
|
|
|
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-10-09 05:16:48 +02:00
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload, state: Rc<S>) -> Task {
|
2017-10-07 06:48:14 +02:00
|
|
|
if let Some(handler) = self.routes.get(req.method()) {
|
|
|
|
handler.handle(req, payload, state)
|
|
|
|
} else {
|
|
|
|
self.default.handle(req, payload, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
|
2017-10-10 08:07:32 +02:00
|
|
|
enum ReplyItem<A> where A: Actor + Route {
|
2017-10-13 23:43:17 +02:00
|
|
|
Message(HttpResponse),
|
2017-10-07 06:48:14 +02:00
|
|
|
Actor(A),
|
|
|
|
}
|
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// Represents response process.
|
2017-10-10 08:07:32 +02:00
|
|
|
pub struct Reply<A: Actor + Route> (ReplyItem<A>);
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-10 08:07:32 +02:00
|
|
|
impl<A> Reply<A> where A: Actor + Route
|
2017-10-07 06:48:14 +02:00
|
|
|
{
|
|
|
|
/// Create async response
|
2017-10-07 08:36:36 +02:00
|
|
|
pub fn stream(act: A) -> Self {
|
2017-10-08 23:56:51 +02:00
|
|
|
Reply(ReplyItem::Actor(act))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 09:31:40 +02:00
|
|
|
/// Send response
|
2017-10-13 23:43:17 +02:00
|
|
|
pub fn reply<R: Into<HttpResponse>>(response: R) -> Self {
|
|
|
|
Reply(ReplyItem::Message(response.into()))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 08:07:32 +02:00
|
|
|
pub fn into(self, mut ctx: HttpContext<A>) -> Task where A: Actor<Context=HttpContext<A>>
|
|
|
|
{
|
2017-10-07 06:48:14 +02:00
|
|
|
match self.0 {
|
2017-10-13 23:43:17 +02:00
|
|
|
ReplyItem::Message(msg) => {
|
|
|
|
Task::reply(msg)
|
2017-10-07 08:14:13 +02:00
|
|
|
},
|
2017-10-08 23:56:51 +02:00
|
|
|
ReplyItem::Actor(act) => {
|
2017-10-07 08:14:13 +02:00
|
|
|
ctx.set_actor(act);
|
2017-10-07 06:48:14 +02:00
|
|
|
Task::with_stream(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 23:43:17 +02:00
|
|
|
|
|
|
|
impl<A, T> From<T> for Reply<A>
|
|
|
|
where T: Into<HttpResponse>, A: Actor + Route
|
|
|
|
{
|
|
|
|
fn from(item: T) -> Self {
|
|
|
|
Reply::reply(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="nightly")]
|
|
|
|
use std::ops::Try;
|
|
|
|
|
|
|
|
#[cfg(feature="nightly")]
|
|
|
|
impl<A> Try for Reply<A> where A: Actor + Route {
|
|
|
|
type Ok = HttpResponse;
|
|
|
|
type Error = HttpResponse;
|
|
|
|
|
|
|
|
fn into_result(self) -> Result<Self::Ok, Self::Error> {
|
|
|
|
panic!("Reply -> Result conversion is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_error(v: Self::Error) -> Self {
|
|
|
|
Reply::reply(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_ok(v: Self::Ok) -> Self {
|
|
|
|
Reply::reply(v)
|
|
|
|
}
|
|
|
|
}
|