mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
introduce custom FromRequest traint for conversion into Reply
This commit is contained in:
parent
187948ddd1
commit
61744b68a1
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use futures::Future;
|
||||
|
||||
use error::Error;
|
||||
use route::{RouteHandler, Reply, Handler, WrapHandler, AsyncHandler};
|
||||
use route::{RouteHandler, Reply, Handler, FromRequest, WrapHandler, AsyncHandler};
|
||||
use resource::Resource;
|
||||
use recognizer::{RouteRecognizer, check_pattern};
|
||||
use httprequest::HttpRequest;
|
||||
@ -190,7 +190,7 @@ impl<S> ApplicationBuilder<S> where S: 'static {
|
||||
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
|
||||
where P: Into<String>,
|
||||
F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static
|
||||
R: FromRequest + 'static
|
||||
{
|
||||
self.parts.as_mut().expect("Use after finish")
|
||||
.handlers.insert(path.into(), Box::new(WrapHandler::new(handler)));
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
// dev specific
|
||||
pub use pipeline::Pipeline;
|
||||
pub use route::Handler;
|
||||
pub use route::{Handler, FromRequest};
|
||||
pub use channel::{HttpChannel, HttpHandler};
|
||||
pub use recognizer::{FromParam, RouteRecognizer};
|
||||
|
||||
|
@ -3,8 +3,7 @@
|
||||
use http::StatusCode;
|
||||
|
||||
use body::Body;
|
||||
use route::Reply;
|
||||
use route::RouteHandler;
|
||||
use route::{Reply, RouteHandler, FromRequest};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::{HttpResponse, HttpResponseBuilder};
|
||||
|
||||
@ -74,6 +73,12 @@ impl<S> RouteHandler<S> for StaticResponse {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for StaticResponse {
|
||||
fn from_request(self, _: HttpRequest) -> Reply {
|
||||
Reply::response(HttpResponse::new(self.0, Body::Empty))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StaticResponse> for HttpResponse {
|
||||
fn from(st: StaticResponse) -> Self {
|
||||
st.response()
|
||||
|
@ -83,6 +83,11 @@ impl HttpRequest<()> {
|
||||
|
||||
impl<S> HttpRequest<S> {
|
||||
|
||||
/// Construct new http request without state.
|
||||
pub fn clone_without_state(&self) -> HttpRequest {
|
||||
HttpRequest(Rc::clone(&self.0), Rc::new(()))
|
||||
}
|
||||
|
||||
/// get mutable reference for inner message
|
||||
fn as_mut(&mut self) -> &mut HttpMessage {
|
||||
let r: &HttpMessage = self.0.as_ref();
|
||||
|
@ -5,7 +5,7 @@ use http::Method;
|
||||
use futures::Future;
|
||||
|
||||
use error::Error;
|
||||
use route::{Reply, Handler, RouteHandler, AsyncHandler, WrapHandler};
|
||||
use route::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
|
||||
use httprequest::HttpRequest;
|
||||
use httpresponse::HttpResponse;
|
||||
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
||||
@ -53,14 +53,14 @@ impl<S> Resource<S> where S: 'static {
|
||||
}
|
||||
|
||||
/// Set resource name
|
||||
pub fn set_name<T: Into<String>>(&mut self, name: T) {
|
||||
pub fn name<T: Into<String>>(&mut self, name: T) {
|
||||
self.name = name.into();
|
||||
}
|
||||
|
||||
/// Register handler for specified method.
|
||||
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static,
|
||||
R: FromRequest + 'static,
|
||||
{
|
||||
self.routes.insert(method, Box::new(WrapHandler::new(handler)));
|
||||
}
|
||||
@ -83,28 +83,28 @@ impl<S> Resource<S> where S: 'static {
|
||||
/// Register handler for `GET` method.
|
||||
pub fn get<F, R>(&mut self, handler: F)
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static, {
|
||||
R: FromRequest + 'static, {
|
||||
self.routes.insert(Method::GET, Box::new(WrapHandler::new(handler)));
|
||||
}
|
||||
|
||||
/// Register handler for `POST` method.
|
||||
pub fn post<F, R>(&mut self, handler: F)
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static, {
|
||||
R: FromRequest + 'static, {
|
||||
self.routes.insert(Method::POST, Box::new(WrapHandler::new(handler)));
|
||||
}
|
||||
|
||||
/// Register handler for `PUT` method.
|
||||
pub fn put<F, R>(&mut self, handler: F)
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static, {
|
||||
R: FromRequest + 'static, {
|
||||
self.routes.insert(Method::PUT, Box::new(WrapHandler::new(handler)));
|
||||
}
|
||||
|
||||
/// Register handler for `DELETE` method.
|
||||
pub fn delete<F, R>(&mut self, handler: F)
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static, {
|
||||
R: FromRequest + 'static, {
|
||||
self.routes.insert(Method::DELETE, Box::new(WrapHandler::new(handler)));
|
||||
}
|
||||
}
|
||||
|
87
src/route.rs
87
src/route.rs
@ -14,16 +14,20 @@ use httpresponse::HttpResponse;
|
||||
pub trait Handler<S>: 'static {
|
||||
|
||||
/// The type of value that handler will return.
|
||||
type Result: Into<Reply>;
|
||||
type Result: FromRequest;
|
||||
|
||||
/// Handle request
|
||||
fn handle(&self, req: HttpRequest<S>) -> Self::Result;
|
||||
}
|
||||
|
||||
pub trait FromRequest {
|
||||
fn from_request(self, req: HttpRequest) -> Reply;
|
||||
}
|
||||
|
||||
/// Handler<S> for Fn()
|
||||
impl<F, R, S> Handler<S> for F
|
||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||
R: Into<Reply> + 'static
|
||||
R: FromRequest + 'static
|
||||
{
|
||||
type Result = R;
|
||||
|
||||
@ -67,28 +71,41 @@ impl Reply {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(actix_nightly))]
|
||||
impl<T: Into<HttpResponse>> From<T> for Reply
|
||||
{
|
||||
fn from(item: T) -> Self {
|
||||
Reply(ReplyItem::Message(item.into()))
|
||||
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<T: Into<HttpResponse>> From<T> for Reply
|
||||
default impl<T: FromRequest> FromRequest for T
|
||||
{
|
||||
fn from(item: T) -> Self {
|
||||
Reply(ReplyItem::Message(item.into()))
|
||||
fn from_request(self, req: HttpRequest) -> Reply {
|
||||
self.from_request(req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(actix_nightly)]
|
||||
default impl<T: Into<HttpResponse>, E: Into<Error>> From<StdResult<T, E>> for Reply {
|
||||
fn from(res: StdResult<T, E>) -> Self {
|
||||
match res {
|
||||
Ok(val) => val.into().into(),
|
||||
Err(err) => err.into().into(),
|
||||
default impl<T: FromRequest, E: Into<Error>> FromRequest for StdResult<T, E> {
|
||||
fn from_request(self, req: HttpRequest) -> Reply {
|
||||
match self {
|
||||
Ok(val) => 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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,22 +114,37 @@ 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(),
|
||||
Err(err) => Reply(ReplyItem::Message(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)))
|
||||
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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Future<Item=HttpResponse, Error=Error>>> for Reply
|
||||
impl<A: Actor<Context=HttpContext<A, S>>, S: 'static> FromRequest for HttpContext<A, S>
|
||||
{
|
||||
fn from(item: Box<Future<Item=HttpResponse, Error=Error>>) -> Self {
|
||||
Reply(ReplyItem::Future(item))
|
||||
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)))
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for Box<Future<Item=HttpResponse, Error=Error>>
|
||||
{
|
||||
fn from_request(self, _: HttpRequest) -> Reply {
|
||||
Reply(ReplyItem::Future(self))
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,7 +157,7 @@ pub(crate) trait RouteHandler<S>: 'static {
|
||||
pub(crate)
|
||||
struct WrapHandler<S, H, R>
|
||||
where H: Handler<S, Result=R>,
|
||||
R: Into<Reply>,
|
||||
R: FromRequest,
|
||||
S: 'static,
|
||||
{
|
||||
h: H,
|
||||
@ -134,7 +166,7 @@ struct WrapHandler<S, H, R>
|
||||
|
||||
impl<S, H, R> WrapHandler<S, H, R>
|
||||
where H: Handler<S, Result=R>,
|
||||
R: Into<Reply>,
|
||||
R: FromRequest,
|
||||
S: 'static,
|
||||
{
|
||||
pub fn new(h: H) -> Self {
|
||||
@ -144,11 +176,12 @@ impl<S, H, R> WrapHandler<S, H, R>
|
||||
|
||||
impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
|
||||
where H: Handler<S, Result=R>,
|
||||
R: Into<Reply> + 'static,
|
||||
R: FromRequest + 'static,
|
||||
S: 'static,
|
||||
{
|
||||
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
||||
self.h.handle(req).into()
|
||||
let req2 = req.clone_without_state();
|
||||
self.h.handle(req).from_request(req2)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user