2017-10-07 06:48:14 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use actix::Actor;
|
2017-11-30 23:42:20 +01:00
|
|
|
use futures::Future;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
use error::Error;
|
2017-12-02 00:45:15 +01:00
|
|
|
use context::{HttpContext, IoContext};
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 08:25:32 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2017-11-03 21:35:34 +01:00
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
/// Trait defines object that could be regestered as route handler
|
2017-10-16 10:19:23 +02:00
|
|
|
#[allow(unused_variables)]
|
2017-11-29 22:26:55 +01:00
|
|
|
pub trait Handler<S>: 'static {
|
2017-11-30 00:07:49 +01:00
|
|
|
|
|
|
|
/// The type of value that handler will return.
|
2017-12-03 01:37:21 +01:00
|
|
|
type Result: FromRequest;
|
2017-11-29 22:26:55 +01:00
|
|
|
|
2017-10-10 08:07:32 +02:00
|
|
|
/// Handle request
|
2017-11-29 22:26:55 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Self::Result;
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:21 +01:00
|
|
|
pub trait FromRequest {
|
2017-12-03 23:22:04 +01:00
|
|
|
/// The associated item which can be returned.
|
|
|
|
type Item: Into<Reply>;
|
|
|
|
|
|
|
|
/// The associated error which can be returned.
|
|
|
|
type Error: Into<Error>;
|
|
|
|
|
|
|
|
fn from_request(self, req: HttpRequest) -> Result<Self::Item, Self::Error>;
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
/// Handler<S> for Fn()
|
|
|
|
impl<F, R, S> Handler<S> for F
|
2017-11-27 06:18:38 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-12-03 01:37:21 +01:00
|
|
|
R: FromRequest + 'static
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
2017-11-29 22:26:55 +01:00
|
|
|
type Result = R;
|
2017-10-15 23:17:41 +02:00
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> R {
|
|
|
|
(self)(req)
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
/// Represents response process.
|
|
|
|
pub struct Reply(ReplyItem);
|
2017-11-29 04:49:17 +01:00
|
|
|
|
2017-12-01 00:13:56 +01:00
|
|
|
pub(crate) enum ReplyItem {
|
2017-11-29 04:49:17 +01:00
|
|
|
Message(HttpResponse),
|
2017-11-30 23:42:20 +01:00
|
|
|
Actor(Box<IoContext>),
|
|
|
|
Future(Box<Future<Item=HttpResponse, Error=Error>>),
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
impl Reply {
|
2017-11-29 04:49:17 +01:00
|
|
|
|
|
|
|
/// Create actor response
|
2017-11-29 19:31:24 +01:00
|
|
|
pub fn actor<A, S>(ctx: HttpContext<A, S>) -> Reply
|
|
|
|
where A: Actor<Context=HttpContext<A, S>>, S: 'static
|
|
|
|
{
|
|
|
|
Reply(ReplyItem::Actor(Box::new(ctx)))
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create async response
|
2017-11-30 23:42:20 +01:00
|
|
|
pub fn async<F>(fut: F) -> Reply
|
|
|
|
where F: Future<Item=HttpResponse, Error=Error> + 'static
|
2017-11-29 04:49:17 +01:00
|
|
|
{
|
2017-11-30 23:42:20 +01:00
|
|
|
Reply(ReplyItem::Future(Box::new(fut)))
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send response
|
2017-12-01 00:13:56 +01:00
|
|
|
pub fn response<R: Into<HttpResponse>>(response: R) -> Reply {
|
2017-11-29 19:31:24 +01:00
|
|
|
Reply(ReplyItem::Message(response.into()))
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:13:56 +01:00
|
|
|
pub(crate) fn into(self) -> ReplyItem {
|
|
|
|
self.0
|
2017-11-29 04:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 01:37:21 +01:00
|
|
|
impl FromRequest for Reply {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn from_request(self, _: HttpRequest) -> Result<Reply, Error> {
|
|
|
|
Ok(self)
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromRequest for HttpResponse {
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn from_request(self, _: HttpRequest) -> Result<Reply, Error> {
|
|
|
|
Ok(Reply(ReplyItem::Message(self)))
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 19:31:24 +01:00
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
impl From<HttpResponse> for Reply {
|
|
|
|
|
|
|
|
fn from(resp: HttpResponse) -> Reply {
|
|
|
|
Reply(ReplyItem::Message(resp))
|
2017-12-02 06:29:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 01:57:25 +01:00
|
|
|
impl<T: FromRequest, E: Into<Error>> FromRequest for Result<T, E>
|
|
|
|
{
|
|
|
|
type Item = <T as FromRequest>::Item;
|
|
|
|
type Error = Error;
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-12-04 01:57:25 +01:00
|
|
|
fn from_request(self, req: HttpRequest) -> Result<Self::Item, Self::Error> {
|
2017-12-03 01:37:21 +01:00
|
|
|
match self {
|
2017-12-04 01:57:25 +01:00
|
|
|
Ok(val) => match val.from_request(req) {
|
|
|
|
Ok(val) => Ok(val),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.into()),
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:22:04 +01:00
|
|
|
impl<E: Into<Error>> From<Result<Reply, E>> for Reply {
|
|
|
|
fn from(res: Result<Reply, E>) -> Self {
|
2017-11-29 19:31:24 +01:00
|
|
|
match res {
|
|
|
|
Ok(val) => val,
|
2017-12-03 01:37:21 +01:00
|
|
|
Err(err) => Reply(ReplyItem::Message(err.into().into())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> FromRequest for HttpContext<A, S>
|
2017-11-29 19:31:24 +01:00
|
|
|
{
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn from_request(self, _: HttpRequest) -> Result<Reply, Error> {
|
|
|
|
Ok(Reply(ReplyItem::Actor(Box::new(self))))
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> From<HttpContext<A, S>> for Reply {
|
2017-12-03 23:22:04 +01:00
|
|
|
|
2017-12-03 01:37:21 +01:00
|
|
|
fn from(ctx: HttpContext<A, S>) -> Reply {
|
|
|
|
Reply(ReplyItem::Actor(Box::new(ctx)))
|
2017-11-29 19:31:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-29 22:26:55 +01:00
|
|
|
|
2017-12-03 01:37:21 +01:00
|
|
|
impl FromRequest for Box<Future<Item=HttpResponse, Error=Error>>
|
2017-12-01 00:13:56 +01:00
|
|
|
{
|
2017-12-03 23:22:04 +01:00
|
|
|
type Item = Reply;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn from_request(self, _: HttpRequest) -> Result<Reply, Error> {
|
|
|
|
Ok(Reply(ReplyItem::Future(self)))
|
2017-12-01 00:13:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 22:26:55 +01:00
|
|
|
/// Trait defines object that could be regestered as resource route
|
|
|
|
pub(crate) trait RouteHandler<S>: 'static {
|
2017-12-01 00:13:56 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply;
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Route handler wrapper for Handler
|
|
|
|
pub(crate)
|
|
|
|
struct WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-03 01:37:21 +01:00
|
|
|
R: FromRequest,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
h: H,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-03 01:37:21 +01:00
|
|
|
R: FromRequest,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(h: H) -> Self {
|
|
|
|
WrapHandler{h: h, s: PhantomData}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
|
|
|
|
where H: Handler<S, Result=R>,
|
2017-12-03 01:37:21 +01:00
|
|
|
R: FromRequest + 'static,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-12-01 00:13:56 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
2017-12-03 01:37:21 +01:00
|
|
|
let req2 = req.clone_without_state();
|
2017-12-03 23:22:04 +01:00
|
|
|
match self.h.handle(req).from_request(req2) {
|
|
|
|
Ok(reply) => reply.into(),
|
|
|
|
Err(err) => Reply::response(err.into()),
|
|
|
|
}
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Async route handler
|
|
|
|
pub(crate)
|
2017-12-02 06:58:19 +01:00
|
|
|
struct AsyncHandler<S, R, F>
|
2017-11-29 22:26:55 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 23:42:20 +01:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
f: Box<F>,
|
|
|
|
s: PhantomData<S>,
|
|
|
|
}
|
|
|
|
|
2017-12-02 06:58:19 +01:00
|
|
|
impl<S, R, F> AsyncHandler<S, R, F>
|
2017-11-29 22:26:55 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 23:42:20 +01:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
2017-12-02 06:58:19 +01:00
|
|
|
AsyncHandler{f: Box::new(f), s: PhantomData}
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 06:58:19 +01:00
|
|
|
impl<S, R, F> RouteHandler<S> for AsyncHandler<S, R, F>
|
2017-11-29 22:26:55 +01:00
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-11-30 23:42:20 +01:00
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
2017-11-29 22:26:55 +01:00
|
|
|
S: 'static,
|
|
|
|
{
|
2017-12-01 00:13:56 +01:00
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
|
|
|
Reply::async((self.f)(req))
|
2017-11-29 22:26:55 +01:00
|
|
|
}
|
|
|
|
}
|