use std::marker::PhantomData; use std::result::Result as StdResult; use actix::Actor; use futures::Future; use error::Error; use context::{HttpContext, IoContext}; use httprequest::HttpRequest; use httpresponse::HttpResponse; /// Trait defines object that could be regestered as route handler #[allow(unused_variables)] pub trait Handler: 'static { /// The type of value that handler will return. type Result: FromRequest; /// Handle request fn handle(&self, req: HttpRequest) -> Self::Result; } pub trait FromRequest { fn from_request(self, req: HttpRequest) -> Reply; } /// Handler for Fn() impl Handler for F where F: Fn(HttpRequest) -> R + 'static, R: FromRequest + 'static { type Result = R; fn handle(&self, req: HttpRequest) -> R { (self)(req) } } /// Represents response process. pub struct Reply(ReplyItem); pub(crate) enum ReplyItem { Message(HttpResponse), Actor(Box), Future(Box>), } impl Reply { /// Create actor response pub fn actor(ctx: HttpContext) -> Reply where A: Actor>, S: 'static { Reply(ReplyItem::Actor(Box::new(ctx))) } /// Create async response pub fn async(fut: F) -> Reply where F: Future + 'static { Reply(ReplyItem::Future(Box::new(fut))) } /// Send response pub fn response>(response: R) -> Reply { Reply(ReplyItem::Message(response.into())) } pub(crate) fn into(self) -> ReplyItem { self.0 } } impl FromRequest for Reply { fn from_request(self, _: HttpRequest) -> Reply { self } } impl FromRequest for HttpResponse { fn from_request(self, _: HttpRequest) -> Reply { Reply(ReplyItem::Message(self)) } } #[cfg(actix_nightly)] default impl FromRequest for T { fn from_request(self, req: HttpRequest) -> Reply { self.from_request(req) } } #[cfg(actix_nightly)] default impl, E: Into> FromRequest for StdResult { fn from_request(self, req: HttpRequest) -> Reply { match self { Ok(val) => Reply(ReplyItem::Message(val.into())), //val.from_request(req), Err(err) => Reply(ReplyItem::Message(err.into().into())), } } } impl> FromRequest for StdResult { fn from_request(self, _: HttpRequest) -> Reply { match self { Ok(val) => val, Err(err) => Reply(ReplyItem::Message(err.into().into())), } } } impl> From> for Reply { fn from(res: StdResult) -> Self { match res { Ok(val) => val, Err(err) => Reply(ReplyItem::Message(err.into().into())), } } } impl> FromRequest for StdResult { fn from_request(self, _: HttpRequest) -> Reply { match self { Ok(val) => Reply(ReplyItem::Message(val)), Err(err) => Reply(ReplyItem::Message(err.into().into())), } } } impl>, S: 'static> FromRequest for HttpContext { fn from_request(self, _: HttpRequest) -> Reply { Reply(ReplyItem::Actor(Box::new(self))) } } impl>, S: 'static> From> for Reply { fn from(ctx: HttpContext) -> Reply { Reply(ReplyItem::Actor(Box::new(ctx))) } } impl FromRequest for Box> { fn from_request(self, _: HttpRequest) -> Reply { Reply(ReplyItem::Future(self)) } } /// Trait defines object that could be regestered as resource route pub(crate) trait RouteHandler: 'static { fn handle(&self, req: HttpRequest) -> Reply; } /// Route handler wrapper for Handler pub(crate) struct WrapHandler where H: Handler, R: FromRequest, S: 'static, { h: H, s: PhantomData, } impl WrapHandler where H: Handler, R: FromRequest, S: 'static, { pub fn new(h: H) -> Self { WrapHandler{h: h, s: PhantomData} } } impl RouteHandler for WrapHandler where H: Handler, R: FromRequest + 'static, S: 'static, { fn handle(&self, req: HttpRequest) -> Reply { let req2 = req.clone_without_state(); self.h.handle(req).from_request(req2) } } /// Async route handler pub(crate) struct AsyncHandler where F: Fn(HttpRequest) -> R + 'static, R: Future + 'static, S: 'static, { f: Box, s: PhantomData, } impl AsyncHandler where F: Fn(HttpRequest) -> R + 'static, R: Future + 'static, S: 'static, { pub fn new(f: F) -> Self { AsyncHandler{f: Box::new(f), s: PhantomData} } } impl RouteHandler for AsyncHandler where F: Fn(HttpRequest) -> R + 'static, R: Future + 'static, S: 'static, { fn handle(&self, req: HttpRequest) -> Reply { Reply::async((self.f)(req)) } }