1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-09-01 01:16:59 +02:00
This commit is contained in:
Nikolay Kim
2017-10-06 23:14:13 -07:00
parent 0b5f0c4f22
commit a505be9321
13 changed files with 106 additions and 65 deletions

View File

@@ -1,4 +1,3 @@
use std::mem;
use std::rc::Rc;
use std::marker::PhantomData;
use std::collections::HashMap;
@@ -11,7 +10,7 @@ use task::Task;
use route::{Route, Payload, RouteHandler};
use context::HttpContext;
use httpcodes::HTTPMethodNotAllowed;
use httpmessage::{HttpRequest, HttpMessage, IntoHttpMessage};
use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse};
/// Resource
pub struct HttpResource<S=()> {
@@ -32,6 +31,7 @@ impl<S> Default for HttpResource<S> {
impl<S> HttpResource<S> where S: 'static {
/// Register handler for specified method.
pub fn handler<H>(&mut self, method: Method, handler: H) -> &mut Self
where H: RouteHandler<S>
{
@@ -39,6 +39,8 @@ impl<S> HttpResource<S> where S: 'static {
self
}
/// Default handler is used if no matched route found.
/// By default `HTTPMethodNotAllowed` is used.
pub fn default_handler<H>(&mut self, handler: H) -> &mut Self
where H: RouteHandler<S>
{
@@ -46,21 +48,25 @@ impl<S> HttpResource<S> where S: 'static {
self
}
/// Handler for `GET` method.
pub fn get<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::GET, A::factory())
}
/// Handler for `POST` method.
pub fn post<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::POST, A::factory())
}
/// Handler for `PUR` method.
pub fn put<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::PUT, A::factory())
}
/// Handler for `METHOD` method.
pub fn delete<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::DELETE, A::factory())
@@ -81,40 +87,40 @@ impl<S: 'static> RouteHandler<S> for HttpResource<S> {
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
enum HttpResponseItem<A> where A: Actor<Context=HttpContext<A>> + Route {
Message(HttpMessage, Option<Bytes>),
enum HttpMessageItem<A> where A: Actor<Context=HttpContext<A>> + Route {
Message(HttpResponse, Option<Bytes>),
Actor(A),
}
pub struct HttpResponse<A: Actor<Context=HttpContext<A>> + Route> (HttpResponseItem<A>);
pub struct HttpMessage<A: Actor<Context=HttpContext<A>> + Route> (HttpMessageItem<A>);
impl<A> HttpResponse<A> where A: Actor<Context=HttpContext<A>> + Route
impl<A> HttpMessage<A> where A: Actor<Context=HttpContext<A>> + Route
{
/// Create async response
#[allow(non_snake_case)]
pub fn Stream(act: A) -> Self {
HttpResponse(HttpResponseItem::Actor(act))
HttpMessage(HttpMessageItem::Actor(act))
}
#[allow(non_snake_case)]
pub fn Reply<I>(req: HttpRequest, msg: I) -> Self
where I: IntoHttpMessage
where I: IntoHttpResponse
{
HttpResponse(HttpResponseItem::Message(msg.into_response(req), None))
HttpMessage(HttpMessageItem::Message(msg.into_response(req), None))
}
#[allow(non_snake_case)]
pub fn ReplyMessage(msg: HttpMessage, body: Option<Bytes>) -> Self {
HttpResponse(HttpResponseItem::Message(msg, body))
pub fn ReplyMessage(msg: HttpResponse, body: Option<Bytes>) -> Self {
HttpMessage(HttpMessageItem::Message(msg, body))
}
pub(crate) fn into(self, mut ctx: HttpContext<A>) -> Task {
match self.0 {
HttpResponseItem::Message(msg, body) =>
Task::reply(msg, body),
HttpResponseItem::Actor(act) => {
let old = ctx.replace_actor(act);
mem::forget(old);
HttpMessageItem::Message(msg, body) => {
Task::reply(msg, body)
},
HttpMessageItem::Actor(act) => {
ctx.set_actor(act);
Task::with_stream(ctx)
}
}