1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/src/route.rs

189 lines
4.3 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
use std::rc::Rc;
2017-10-29 14:05:31 +01:00
use std::cell::RefCell;
2017-10-07 06:48:14 +02:00
use std::marker::PhantomData;
2017-11-29 19:31:24 +01:00
use std::result::Result as StdResult;
2017-10-07 06:48:14 +02:00
use actix::Actor;
2017-10-15 23:17:41 +02:00
use futures::Stream;
2017-10-07 06:48:14 +02:00
2017-11-10 22:42:32 +01:00
use body::Binary;
2017-11-29 22:26:55 +01:00
use error::Error;
2017-10-07 06:48:14 +02:00
use context::HttpContext;
use httprequest::HttpRequest;
2017-10-24 08:25:32 +02:00
use httpresponse::HttpResponse;
2017-11-29 22:26:55 +01:00
use task::{Task, DrainFut, IoContext};
2017-10-07 06:48:14 +02:00
2017-10-07 08:14:13 +02:00
#[doc(hidden)]
2017-10-07 06:48:14 +02:00
#[derive(Debug)]
pub enum Frame {
Message(HttpResponse),
2017-11-10 22:42:32 +01:00
Payload(Option<Binary>),
2017-10-29 14:05:31 +01:00
Drain(Rc<RefCell<DrainFut>>),
2017-10-07 06:48:14 +02:00
}
2017-11-03 21:35:34 +01:00
impl Frame {
pub fn eof() -> Frame {
Frame::Payload(None)
}
}
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-11-29 22:26:55 +01:00
type Result: Into<Reply>;
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-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-11-29 22:26:55 +01:00
R: Into<Reply> + '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
enum ReplyItem {
Message(HttpResponse),
Actor(Box<IoContext<Item=Frame, Error=Error>>),
Stream(Box<Stream<Item=Frame, Error=Error>>),
}
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-29 19:31:24 +01:00
pub fn stream<S>(stream: S) -> Reply
2017-11-29 04:49:17 +01:00
where S: Stream<Item=Frame, Error=Error> + 'static
{
2017-11-29 19:31:24 +01:00
Reply(ReplyItem::Stream(Box::new(stream)))
2017-11-29 04:49:17 +01:00
}
/// Send response
2017-11-29 19:31:24 +01:00
pub fn reply<R: Into<HttpResponse>>(response: R) -> Reply {
Reply(ReplyItem::Message(response.into()))
2017-11-29 04:49:17 +01:00
}
pub fn into(self, task: &mut Task)
{
match self.0 {
ReplyItem::Message(msg) => {
task.reply(msg)
},
ReplyItem::Actor(ctx) => {
task.context(ctx)
}
ReplyItem::Stream(stream) => {
task.stream(stream)
}
}
}
}
2017-11-29 18:17:00 +01:00
impl<T: Into<HttpResponse>> From<T> for Reply
2017-11-29 04:49:17 +01:00
{
fn from(item: T) -> Self {
Reply(ReplyItem::Message(item.into()))
2017-10-15 23:17:41 +02:00
}
}
2017-11-29 19:31:24 +01: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) => err.into().into(),
}
}
}
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> From<HttpContext<A, S>> for Reply
{
fn from(item: HttpContext<A, S>) -> Self {
Reply(ReplyItem::Actor(Box::new(item)))
}
}
2017-11-29 22:26:55 +01:00
/// Trait defines object that could be regestered as resource route
pub(crate) trait RouteHandler<S>: 'static {
/// Handle request
fn handle(&self, req: HttpRequest<S>, task: &mut Task);
}
/// Route handler wrapper for Handler
pub(crate)
struct WrapHandler<S, H, R>
where H: Handler<S, Result=R>,
R: Into<Reply>,
S: 'static,
{
h: H,
s: PhantomData<S>,
}
impl<S, H, R> WrapHandler<S, H, R>
where H: Handler<S, Result=R>,
R: Into<Reply>,
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: Into<Reply> + 'static,
S: 'static,
{
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
self.h.handle(req).into().into(task)
}
}
/// Async route handler
pub(crate)
struct StreamHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static,
{
f: Box<F>,
s: PhantomData<S>,
}
impl<S, R, F> StreamHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static,
{
pub fn new(f: F) -> Self {
StreamHandler{f: Box::new(f), s: PhantomData}
}
}
impl<S, R, F> RouteHandler<S> for StreamHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static,
{
fn handle(&self, req: HttpRequest<S>, task: &mut Task) {
task.stream((self.f)(req))
}
}