1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 22:49:21 +02:00

added proc-macros for route registration

This commit is contained in:
Nikolay Kim
2019-03-07 11:09:42 -08:00
parent 1151b5bf7c
commit 22708e78a9
9 changed files with 221 additions and 42 deletions

View File

@ -31,7 +31,7 @@ where
}
#[doc(hidden)]
pub struct Handle<F, T, R>
pub struct Handler<F, T, R>
where
F: Factory<T, R>,
R: Responder,
@ -40,19 +40,19 @@ where
_t: PhantomData<(T, R)>,
}
impl<F, T, R> Handle<F, T, R>
impl<F, T, R> Handler<F, T, R>
where
F: Factory<T, R>,
R: Responder,
{
pub fn new(hnd: F) -> Self {
Handle {
Handler {
hnd,
_t: PhantomData,
}
}
}
impl<F, T, R> NewService<(T, HttpRequest)> for Handle<F, T, R>
impl<F, T, R> NewService<(T, HttpRequest)> for Handler<F, T, R>
where
F: Factory<T, R>,
R: Responder + 'static,
@ -60,11 +60,11 @@ where
type Response = ServiceResponse;
type Error = Void;
type InitError = ();
type Service = HandleService<F, T, R>;
type Service = HandlerService<F, T, R>;
type Future = FutureResult<Self::Service, ()>;
fn new_service(&self, _: &()) -> Self::Future {
ok(HandleService {
ok(HandlerService {
hnd: self.hnd.clone(),
_t: PhantomData,
})
@ -72,7 +72,7 @@ where
}
#[doc(hidden)]
pub struct HandleService<F, T, R>
pub struct HandlerService<F, T, R>
where
F: Factory<T, R>,
R: Responder + 'static,
@ -81,14 +81,14 @@ where
_t: PhantomData<(T, R)>,
}
impl<F, T, R> Service<(T, HttpRequest)> for HandleService<F, T, R>
impl<F, T, R> Service<(T, HttpRequest)> for HandlerService<F, T, R>
where
F: Factory<T, R>,
R: Responder + 'static,
{
type Response = ServiceResponse;
type Error = Void;
type Future = HandleServiceResponse<<R::Future as IntoFuture>::Future>;
type Future = HandlerServiceResponse<<R::Future as IntoFuture>::Future>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
@ -96,19 +96,19 @@ where
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
let fut = self.hnd.call(param).respond_to(&req).into_future();
HandleServiceResponse {
HandlerServiceResponse {
fut,
req: Some(req),
}
}
}
pub struct HandleServiceResponse<T> {
pub struct HandlerServiceResponse<T> {
fut: T,
req: Option<HttpRequest>,
}
impl<T> Future for HandleServiceResponse<T>
impl<T> Future for HandlerServiceResponse<T>
where
T: Future<Item = Response>,
T::Error: Into<Error>,
@ -157,7 +157,7 @@ where
}
#[doc(hidden)]
pub struct AsyncHandle<F, T, R>
pub struct AsyncHandler<F, T, R>
where
F: AsyncFactory<T, R>,
R: IntoFuture,
@ -168,7 +168,7 @@ where
_t: PhantomData<(T, R)>,
}
impl<F, T, R> AsyncHandle<F, T, R>
impl<F, T, R> AsyncHandler<F, T, R>
where
F: AsyncFactory<T, R>,
R: IntoFuture,
@ -176,13 +176,13 @@ where
R::Error: Into<Error>,
{
pub fn new(hnd: F) -> Self {
AsyncHandle {
AsyncHandler {
hnd,
_t: PhantomData,
}
}
}
impl<F, T, R> NewService<(T, HttpRequest)> for AsyncHandle<F, T, R>
impl<F, T, R> NewService<(T, HttpRequest)> for AsyncHandler<F, T, R>
where
F: AsyncFactory<T, R>,
R: IntoFuture,
@ -192,11 +192,11 @@ where
type Response = ServiceResponse;
type Error = ();
type InitError = ();
type Service = AsyncHandleService<F, T, R>;
type Service = AsyncHandlerService<F, T, R>;
type Future = FutureResult<Self::Service, ()>;
fn new_service(&self, _: &()) -> Self::Future {
ok(AsyncHandleService {
ok(AsyncHandlerService {
hnd: self.hnd.clone(),
_t: PhantomData,
})
@ -204,7 +204,7 @@ where
}
#[doc(hidden)]
pub struct AsyncHandleService<F, T, R>
pub struct AsyncHandlerService<F, T, R>
where
F: AsyncFactory<T, R>,
R: IntoFuture,
@ -215,7 +215,7 @@ where
_t: PhantomData<(T, R)>,
}
impl<F, T, R> Service<(T, HttpRequest)> for AsyncHandleService<F, T, R>
impl<F, T, R> Service<(T, HttpRequest)> for AsyncHandlerService<F, T, R>
where
F: AsyncFactory<T, R>,
R: IntoFuture,
@ -224,14 +224,14 @@ where
{
type Response = ServiceResponse;
type Error = ();
type Future = AsyncHandleServiceResponse<R::Future>;
type Future = AsyncHandlerServiceResponse<R::Future>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
AsyncHandleServiceResponse {
AsyncHandlerServiceResponse {
fut: self.hnd.call(param).into_future(),
req: Some(req),
}
@ -239,12 +239,12 @@ where
}
#[doc(hidden)]
pub struct AsyncHandleServiceResponse<T> {
pub struct AsyncHandlerServiceResponse<T> {
fut: T,
req: Option<HttpRequest>,
}
impl<T> Future for AsyncHandleServiceResponse<T>
impl<T> Future for AsyncHandlerServiceResponse<T>
where
T: Future,
T::Item: Into<Response>,

View File

@ -18,6 +18,24 @@ mod service;
mod state;
pub mod test;
/// Attribute macros for route registration
///
/// ```rust
/// use actix_web::{macros, App, HttpResponse};
///
/// #[macros::get("/index.html")]
/// fn index() -> HttpResponse {
/// HttpResponse::Ok().finish()
/// }
///
/// fn main() {
/// let app = App::new().service(index);
/// }
/// ```
pub mod macros {
pub use actix_web_codegen::{get, post, put};
}
// re-export for convenience
pub use actix_http::Response as HttpResponse;
pub use actix_http::{body, error, http, Error, HttpMessage, ResponseError, Result};

View File

@ -8,7 +8,7 @@ use futures::{Async, Future, IntoFuture, Poll};
use crate::extract::{ConfigStorage, ExtractorConfig, FromRequest};
use crate::guard::{self, Guard};
use crate::handler::{AsyncFactory, AsyncHandle, Extract, Factory, Handle};
use crate::handler::{AsyncFactory, AsyncHandler, Extract, Factory, Handler};
use crate::responder::Responder;
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
use crate::HttpResponse;
@ -50,8 +50,9 @@ impl<P: 'static> Route<P> {
let config_ref = Rc::new(RefCell::new(None));
Route {
service: Box::new(RouteNewService::new(
Extract::new(config_ref.clone())
.and_then(Handle::new(HttpResponse::NotFound).map_err(|_| panic!())),
Extract::new(config_ref.clone()).and_then(
Handler::new(HttpResponse::NotFound).map_err(|_| panic!()),
),
)),
guards: Rc::new(Vec::new()),
config: ConfigStorage::default(),
@ -272,7 +273,7 @@ impl<P: 'static> Route<P> {
T::Config::store_default(&mut self.config);
self.service = Box::new(RouteNewService::new(
Extract::new(self.config_ref.clone())
.and_then(Handle::new(handler).map_err(|_| panic!())),
.and_then(Handler::new(handler).map_err(|_| panic!())),
));
self
}
@ -314,7 +315,7 @@ impl<P: 'static> Route<P> {
{
self.service = Box::new(RouteNewService::new(
Extract::new(self.config_ref.clone())
.and_then(AsyncHandle::new(handler).map_err(|_| panic!())),
.and_then(AsyncHandler::new(handler).map_err(|_| panic!())),
));
self
}