mirror of
https://github.com/actix/actix-extras.git
synced 2024-12-02 19:12:24 +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 futures::Future;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use route::{RouteHandler, Reply, Handler, WrapHandler, AsyncHandler};
|
use route::{RouteHandler, Reply, Handler, FromRequest, WrapHandler, AsyncHandler};
|
||||||
use resource::Resource;
|
use resource::Resource;
|
||||||
use recognizer::{RouteRecognizer, check_pattern};
|
use recognizer::{RouteRecognizer, check_pattern};
|
||||||
use httprequest::HttpRequest;
|
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
|
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
|
||||||
where P: Into<String>,
|
where P: Into<String>,
|
||||||
F: Fn(HttpRequest<S>) -> R + 'static,
|
F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static
|
R: FromRequest + 'static
|
||||||
{
|
{
|
||||||
self.parts.as_mut().expect("Use after finish")
|
self.parts.as_mut().expect("Use after finish")
|
||||||
.handlers.insert(path.into(), Box::new(WrapHandler::new(handler)));
|
.handlers.insert(path.into(), Box::new(WrapHandler::new(handler)));
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// dev specific
|
// dev specific
|
||||||
pub use pipeline::Pipeline;
|
pub use pipeline::Pipeline;
|
||||||
pub use route::Handler;
|
pub use route::{Handler, FromRequest};
|
||||||
pub use channel::{HttpChannel, HttpHandler};
|
pub use channel::{HttpChannel, HttpHandler};
|
||||||
pub use recognizer::{FromParam, RouteRecognizer};
|
pub use recognizer::{FromParam, RouteRecognizer};
|
||||||
|
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
|
||||||
use body::Body;
|
use body::Body;
|
||||||
use route::Reply;
|
use route::{Reply, RouteHandler, FromRequest};
|
||||||
use route::RouteHandler;
|
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::{HttpResponse, HttpResponseBuilder};
|
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 {
|
impl From<StaticResponse> for HttpResponse {
|
||||||
fn from(st: StaticResponse) -> Self {
|
fn from(st: StaticResponse) -> Self {
|
||||||
st.response()
|
st.response()
|
||||||
|
@ -83,6 +83,11 @@ impl HttpRequest<()> {
|
|||||||
|
|
||||||
impl<S> HttpRequest<S> {
|
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
|
/// get mutable reference for inner message
|
||||||
fn as_mut(&mut self) -> &mut HttpMessage {
|
fn as_mut(&mut self) -> &mut HttpMessage {
|
||||||
let r: &HttpMessage = self.0.as_ref();
|
let r: &HttpMessage = self.0.as_ref();
|
||||||
|
@ -5,7 +5,7 @@ use http::Method;
|
|||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use route::{Reply, Handler, RouteHandler, AsyncHandler, WrapHandler};
|
use route::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
|
||||||
@ -53,14 +53,14 @@ impl<S> Resource<S> where S: 'static {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set resource name
|
/// 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();
|
self.name = name.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register handler for specified method.
|
/// Register handler for specified method.
|
||||||
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
pub fn handler<F, R>(&mut self, method: Method, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static,
|
R: FromRequest + 'static,
|
||||||
{
|
{
|
||||||
self.routes.insert(method, Box::new(WrapHandler::new(handler)));
|
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.
|
/// Register handler for `GET` method.
|
||||||
pub fn get<F, R>(&mut self, handler: F)
|
pub fn get<F, R>(&mut self, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static, {
|
R: FromRequest + 'static, {
|
||||||
self.routes.insert(Method::GET, Box::new(WrapHandler::new(handler)));
|
self.routes.insert(Method::GET, Box::new(WrapHandler::new(handler)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register handler for `POST` method.
|
/// Register handler for `POST` method.
|
||||||
pub fn post<F, R>(&mut self, handler: F)
|
pub fn post<F, R>(&mut self, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static, {
|
R: FromRequest + 'static, {
|
||||||
self.routes.insert(Method::POST, Box::new(WrapHandler::new(handler)));
|
self.routes.insert(Method::POST, Box::new(WrapHandler::new(handler)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register handler for `PUT` method.
|
/// Register handler for `PUT` method.
|
||||||
pub fn put<F, R>(&mut self, handler: F)
|
pub fn put<F, R>(&mut self, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static, {
|
R: FromRequest + 'static, {
|
||||||
self.routes.insert(Method::PUT, Box::new(WrapHandler::new(handler)));
|
self.routes.insert(Method::PUT, Box::new(WrapHandler::new(handler)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register handler for `DELETE` method.
|
/// Register handler for `DELETE` method.
|
||||||
pub fn delete<F, R>(&mut self, handler: F)
|
pub fn delete<F, R>(&mut self, handler: F)
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static, {
|
R: FromRequest + 'static, {
|
||||||
self.routes.insert(Method::DELETE, Box::new(WrapHandler::new(handler)));
|
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 {
|
pub trait Handler<S>: 'static {
|
||||||
|
|
||||||
/// The type of value that handler will return.
|
/// The type of value that handler will return.
|
||||||
type Result: Into<Reply>;
|
type Result: FromRequest;
|
||||||
|
|
||||||
/// Handle request
|
/// Handle request
|
||||||
fn handle(&self, req: HttpRequest<S>) -> Self::Result;
|
fn handle(&self, req: HttpRequest<S>) -> Self::Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait FromRequest {
|
||||||
|
fn from_request(self, req: HttpRequest) -> Reply;
|
||||||
|
}
|
||||||
|
|
||||||
/// Handler<S> for Fn()
|
/// Handler<S> for Fn()
|
||||||
impl<F, R, S> Handler<S> for F
|
impl<F, R, S> Handler<S> for F
|
||||||
where F: Fn(HttpRequest<S>) -> R + 'static,
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
||||||
R: Into<Reply> + 'static
|
R: FromRequest + 'static
|
||||||
{
|
{
|
||||||
type Result = R;
|
type Result = R;
|
||||||
|
|
||||||
@ -67,28 +71,41 @@ impl Reply {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(actix_nightly))]
|
impl FromRequest for Reply {
|
||||||
impl<T: Into<HttpResponse>> From<T> for Reply
|
fn from_request(self, _: HttpRequest) -> Reply {
|
||||||
{
|
self
|
||||||
fn from(item: T) -> Self {
|
}
|
||||||
Reply(ReplyItem::Message(item.into()))
|
}
|
||||||
|
|
||||||
|
impl FromRequest for HttpResponse {
|
||||||
|
fn from_request(self, _: HttpRequest) -> Reply {
|
||||||
|
Reply(ReplyItem::Message(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(actix_nightly)]
|
#[cfg(actix_nightly)]
|
||||||
default impl<T: Into<HttpResponse>> From<T> for Reply
|
default impl<T: FromRequest> FromRequest for T
|
||||||
{
|
{
|
||||||
fn from(item: T) -> Self {
|
fn from_request(self, req: HttpRequest) -> Reply {
|
||||||
Reply(ReplyItem::Message(item.into()))
|
self.from_request(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(actix_nightly)]
|
#[cfg(actix_nightly)]
|
||||||
default impl<T: Into<HttpResponse>, E: Into<Error>> From<StdResult<T, E>> for Reply {
|
default impl<T: FromRequest, E: Into<Error>> FromRequest for StdResult<T, E> {
|
||||||
fn from(res: StdResult<T, E>) -> Self {
|
fn from_request(self, req: HttpRequest) -> Reply {
|
||||||
match res {
|
match self {
|
||||||
Ok(val) => val.into().into(),
|
Ok(val) => val.from_request(req),
|
||||||
Err(err) => err.into().into(),
|
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 {
|
fn from(res: StdResult<Reply, E>) -> Self {
|
||||||
match res {
|
match res {
|
||||||
Ok(val) => val,
|
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
|
impl<E: Into<Error>> FromRequest for StdResult<HttpResponse, E> {
|
||||||
{
|
fn from_request(self, _: HttpRequest) -> Reply {
|
||||||
fn from(item: HttpContext<A, S>) -> Self {
|
match self {
|
||||||
Reply(ReplyItem::Actor(Box::new(item)))
|
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 {
|
fn from_request(self, _: HttpRequest) -> Reply {
|
||||||
Reply(ReplyItem::Future(item))
|
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)
|
pub(crate)
|
||||||
struct WrapHandler<S, H, R>
|
struct WrapHandler<S, H, R>
|
||||||
where H: Handler<S, Result=R>,
|
where H: Handler<S, Result=R>,
|
||||||
R: Into<Reply>,
|
R: FromRequest,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
h: H,
|
h: H,
|
||||||
@ -134,7 +166,7 @@ struct WrapHandler<S, H, R>
|
|||||||
|
|
||||||
impl<S, H, R> WrapHandler<S, H, R>
|
impl<S, H, R> WrapHandler<S, H, R>
|
||||||
where H: Handler<S, Result=R>,
|
where H: Handler<S, Result=R>,
|
||||||
R: Into<Reply>,
|
R: FromRequest,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
pub fn new(h: H) -> Self {
|
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>
|
impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
|
||||||
where H: Handler<S, Result=R>,
|
where H: Handler<S, Result=R>,
|
||||||
R: Into<Reply> + 'static,
|
R: FromRequest + 'static,
|
||||||
S: 'static,
|
S: 'static,
|
||||||
{
|
{
|
||||||
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
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