1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-21 09:23:54 +00:00
actix-web/src/route.rs

218 lines
5.3 KiB
Rust
Raw Normal View History

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