mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
added proc-macros for route registration
This commit is contained in:
parent
1151b5bf7c
commit
22708e78a9
@ -29,6 +29,7 @@ members = [
|
|||||||
".",
|
".",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-session",
|
"actix-session",
|
||||||
|
"actix-web-codegen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
@ -63,6 +64,7 @@ actix-codec = "0.1.0"
|
|||||||
#actix-service = "0.3.2"
|
#actix-service = "0.3.2"
|
||||||
#actix-utils = "0.3.1"
|
#actix-utils = "0.3.1"
|
||||||
actix-rt = "0.2.0"
|
actix-rt = "0.2.0"
|
||||||
|
actix-web-codegen = { path="actix-web-codegen" }
|
||||||
|
|
||||||
actix-service = { git = "https://github.com/actix/actix-net.git" }
|
actix-service = { git = "https://github.com/actix/actix-net.git" }
|
||||||
actix-utils = { git = "https://github.com/actix/actix-net.git" }
|
actix-utils = { git = "https://github.com/actix/actix-net.git" }
|
||||||
|
@ -318,9 +318,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>>
|
impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>> for Files<P, C> {
|
||||||
for Files<P, C>
|
|
||||||
{
|
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Service = FilesService<P, C>;
|
type Service = FilesService<P, C>;
|
||||||
@ -730,8 +728,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_content_range_headers() {
|
fn test_named_file_content_range_headers() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new()
|
App::new().service(Files::new("/test", ".").index_file("tests/test.binary")),
|
||||||
.service(Files::new("/test", ".").index_file("tests/test.binary")),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
@ -770,8 +767,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_named_file_content_length_headers() {
|
fn test_named_file_content_length_headers() {
|
||||||
let mut srv = test::init_service(
|
let mut srv = test::init_service(
|
||||||
App::new()
|
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||||
.service(Files::new("test", ".").index_file("tests/test.binary")),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Valid range header
|
// Valid range header
|
||||||
|
15
actix-web-codegen/Cargo.toml
Normal file
15
actix-web-codegen/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "actix-web-codegen"
|
||||||
|
description = "Actix web codegen macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
|
license = "MIT/Apache-2.0"
|
||||||
|
edition = "2018"
|
||||||
|
workspace = ".."
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
quote = "0.6"
|
||||||
|
syn = { version = "0.15", features = ["full", "parsing"] }
|
115
actix-web-codegen/src/lib.rs
Normal file
115
actix-web-codegen/src/lib.rs
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
#![recursion_limit = "512"]
|
||||||
|
|
||||||
|
extern crate proc_macro;
|
||||||
|
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::parse_macro_input;
|
||||||
|
|
||||||
|
/// #[get("path")] attribute
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
let args = parse_macro_input!(args as syn::AttributeArgs);
|
||||||
|
if args.is_empty() {
|
||||||
|
panic!("invalid server definition, expected: #[get(\"some path\")]");
|
||||||
|
}
|
||||||
|
|
||||||
|
// path
|
||||||
|
let path = match args[0] {
|
||||||
|
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => {
|
||||||
|
let fname = quote!(#fname).to_string();
|
||||||
|
fname.as_str()[1..fname.len() - 1].to_owned()
|
||||||
|
}
|
||||||
|
_ => panic!("resource path"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast: syn::ItemFn = syn::parse(input).unwrap();
|
||||||
|
let name = ast.ident.clone();
|
||||||
|
|
||||||
|
(quote! {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct #name;
|
||||||
|
|
||||||
|
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name {
|
||||||
|
fn register(self, config: &mut actix_web::dev::AppConfig<P>) {
|
||||||
|
#ast
|
||||||
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
|
actix_web::Resource::new(#path)
|
||||||
|
.route(actix_web::web::get().to(#name)), config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// #[post("path")] attribute
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn post(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
let args = parse_macro_input!(args as syn::AttributeArgs);
|
||||||
|
if args.is_empty() {
|
||||||
|
panic!("invalid server definition, expected: #[get(\"some path\")]");
|
||||||
|
}
|
||||||
|
|
||||||
|
// path
|
||||||
|
let path = match args[0] {
|
||||||
|
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => {
|
||||||
|
let fname = quote!(#fname).to_string();
|
||||||
|
fname.as_str()[1..fname.len() - 1].to_owned()
|
||||||
|
}
|
||||||
|
_ => panic!("resource path"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast: syn::ItemFn = syn::parse(input).unwrap();
|
||||||
|
let name = ast.ident.clone();
|
||||||
|
|
||||||
|
(quote! {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct #name;
|
||||||
|
|
||||||
|
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name {
|
||||||
|
fn register(self, config: &mut actix_web::dev::AppConfig<P>) {
|
||||||
|
#ast
|
||||||
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
|
actix_web::Resource::new(#path)
|
||||||
|
.route(actix_web::web::post().to(#name)), config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// #[put("path")] attribute
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn put(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
|
let args = parse_macro_input!(args as syn::AttributeArgs);
|
||||||
|
if args.is_empty() {
|
||||||
|
panic!("invalid server definition, expected: #[get(\"some path\")]");
|
||||||
|
}
|
||||||
|
|
||||||
|
// path
|
||||||
|
let path = match args[0] {
|
||||||
|
syn::NestedMeta::Literal(syn::Lit::Str(ref fname)) => {
|
||||||
|
let fname = quote!(#fname).to_string();
|
||||||
|
fname.as_str()[1..fname.len() - 1].to_owned()
|
||||||
|
}
|
||||||
|
_ => panic!("resource path"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let ast: syn::ItemFn = syn::parse(input).unwrap();
|
||||||
|
let name = ast.ident.clone();
|
||||||
|
|
||||||
|
(quote! {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
struct #name;
|
||||||
|
|
||||||
|
impl<P: 'static> actix_web::dev::HttpServiceFactory<P> for #name {
|
||||||
|
fn register(self, config: &mut actix_web::dev::AppConfig<P>) {
|
||||||
|
#ast
|
||||||
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
|
actix_web::Resource::new(#path)
|
||||||
|
.route(actix_web::web::put().to(#name)), config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into()
|
||||||
|
}
|
31
actix-web-codegen/src/server.rs
Normal file
31
actix-web-codegen/src/server.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{quote, ToTokens};
|
||||||
|
use syn;
|
||||||
|
|
||||||
|
/// Thrift mux server impl
|
||||||
|
pub struct Server {}
|
||||||
|
|
||||||
|
impl Server {
|
||||||
|
fn new() -> Server {
|
||||||
|
Server {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// generate servers
|
||||||
|
pub fn generate(input: TokenStream) {
|
||||||
|
let mut srv = Server::new();
|
||||||
|
let ast: syn::ItemFn = syn::parse2(input).unwrap();
|
||||||
|
println!("T: {:?}", ast.ident);
|
||||||
|
|
||||||
|
// quote! {
|
||||||
|
// #ast
|
||||||
|
|
||||||
|
// #(#servers)*
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
use futures::IntoFuture;
|
use futures::IntoFuture;
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::macros::get;
|
||||||
http::Method, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||||
};
|
|
||||||
|
|
||||||
|
#[get("/resource1/index.html")]
|
||||||
fn index(req: HttpRequest) -> &'static str {
|
fn index(req: HttpRequest) -> &'static str {
|
||||||
println!("REQ: {:?}", req);
|
println!("REQ: {:?}", req);
|
||||||
"Hello world!\r\n"
|
"Hello world!\r\n"
|
||||||
@ -14,6 +14,7 @@ fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error =
|
|||||||
Ok("Hello world!\r\n")
|
Ok("Hello world!\r\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
fn no_params() -> &'static str {
|
fn no_params() -> &'static str {
|
||||||
"Hello world!\r\n"
|
"Hello world!\r\n"
|
||||||
}
|
}
|
||||||
@ -27,7 +28,8 @@ fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||||
.middleware(middleware::Compress::default())
|
.middleware(middleware::Compress::default())
|
||||||
.service(web::resource("/resource1/index.html").route(web::get().to(index)))
|
.service(index)
|
||||||
|
.service(no_params)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/resource2/index.html")
|
web::resource("/resource2/index.html")
|
||||||
.middleware(
|
.middleware(
|
||||||
@ -36,10 +38,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
.default_resource(|r| {
|
.default_resource(|r| {
|
||||||
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
|
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
|
||||||
})
|
})
|
||||||
.route(web::method(Method::GET).to_async(index_async)),
|
.route(web::get().to_async(index_async)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/test1.html").to(|| "Test\r\n"))
|
.service(web::resource("/test1.html").to(|| "Test\r\n"))
|
||||||
.service(web::resource("/").to(no_params))
|
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.workers(1)
|
.workers(1)
|
||||||
|
@ -31,7 +31,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct Handle<F, T, R>
|
pub struct Handler<F, T, R>
|
||||||
where
|
where
|
||||||
F: Factory<T, R>,
|
F: Factory<T, R>,
|
||||||
R: Responder,
|
R: Responder,
|
||||||
@ -40,19 +40,19 @@ where
|
|||||||
_t: PhantomData<(T, R)>,
|
_t: PhantomData<(T, R)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, R> Handle<F, T, R>
|
impl<F, T, R> Handler<F, T, R>
|
||||||
where
|
where
|
||||||
F: Factory<T, R>,
|
F: Factory<T, R>,
|
||||||
R: Responder,
|
R: Responder,
|
||||||
{
|
{
|
||||||
pub fn new(hnd: F) -> Self {
|
pub fn new(hnd: F) -> Self {
|
||||||
Handle {
|
Handler {
|
||||||
hnd,
|
hnd,
|
||||||
_t: PhantomData,
|
_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
|
where
|
||||||
F: Factory<T, R>,
|
F: Factory<T, R>,
|
||||||
R: Responder + 'static,
|
R: Responder + 'static,
|
||||||
@ -60,11 +60,11 @@ where
|
|||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = Void;
|
type Error = Void;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Service = HandleService<F, T, R>;
|
type Service = HandlerService<F, T, R>;
|
||||||
type Future = FutureResult<Self::Service, ()>;
|
type Future = FutureResult<Self::Service, ()>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(HandleService {
|
ok(HandlerService {
|
||||||
hnd: self.hnd.clone(),
|
hnd: self.hnd.clone(),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
})
|
})
|
||||||
@ -72,7 +72,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct HandleService<F, T, R>
|
pub struct HandlerService<F, T, R>
|
||||||
where
|
where
|
||||||
F: Factory<T, R>,
|
F: Factory<T, R>,
|
||||||
R: Responder + 'static,
|
R: Responder + 'static,
|
||||||
@ -81,14 +81,14 @@ where
|
|||||||
_t: PhantomData<(T, R)>,
|
_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
|
where
|
||||||
F: Factory<T, R>,
|
F: Factory<T, R>,
|
||||||
R: Responder + 'static,
|
R: Responder + 'static,
|
||||||
{
|
{
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = Void;
|
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> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
@ -96,19 +96,19 @@ where
|
|||||||
|
|
||||||
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
||||||
let fut = self.hnd.call(param).respond_to(&req).into_future();
|
let fut = self.hnd.call(param).respond_to(&req).into_future();
|
||||||
HandleServiceResponse {
|
HandlerServiceResponse {
|
||||||
fut,
|
fut,
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HandleServiceResponse<T> {
|
pub struct HandlerServiceResponse<T> {
|
||||||
fut: T,
|
fut: T,
|
||||||
req: Option<HttpRequest>,
|
req: Option<HttpRequest>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Future for HandleServiceResponse<T>
|
impl<T> Future for HandlerServiceResponse<T>
|
||||||
where
|
where
|
||||||
T: Future<Item = Response>,
|
T: Future<Item = Response>,
|
||||||
T::Error: Into<Error>,
|
T::Error: Into<Error>,
|
||||||
@ -157,7 +157,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct AsyncHandle<F, T, R>
|
pub struct AsyncHandler<F, T, R>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R>,
|
F: AsyncFactory<T, R>,
|
||||||
R: IntoFuture,
|
R: IntoFuture,
|
||||||
@ -168,7 +168,7 @@ where
|
|||||||
_t: PhantomData<(T, R)>,
|
_t: PhantomData<(T, R)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T, R> AsyncHandle<F, T, R>
|
impl<F, T, R> AsyncHandler<F, T, R>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R>,
|
F: AsyncFactory<T, R>,
|
||||||
R: IntoFuture,
|
R: IntoFuture,
|
||||||
@ -176,13 +176,13 @@ where
|
|||||||
R::Error: Into<Error>,
|
R::Error: Into<Error>,
|
||||||
{
|
{
|
||||||
pub fn new(hnd: F) -> Self {
|
pub fn new(hnd: F) -> Self {
|
||||||
AsyncHandle {
|
AsyncHandler {
|
||||||
hnd,
|
hnd,
|
||||||
_t: PhantomData,
|
_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
|
where
|
||||||
F: AsyncFactory<T, R>,
|
F: AsyncFactory<T, R>,
|
||||||
R: IntoFuture,
|
R: IntoFuture,
|
||||||
@ -192,11 +192,11 @@ where
|
|||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Service = AsyncHandleService<F, T, R>;
|
type Service = AsyncHandlerService<F, T, R>;
|
||||||
type Future = FutureResult<Self::Service, ()>;
|
type Future = FutureResult<Self::Service, ()>;
|
||||||
|
|
||||||
fn new_service(&self, _: &()) -> Self::Future {
|
fn new_service(&self, _: &()) -> Self::Future {
|
||||||
ok(AsyncHandleService {
|
ok(AsyncHandlerService {
|
||||||
hnd: self.hnd.clone(),
|
hnd: self.hnd.clone(),
|
||||||
_t: PhantomData,
|
_t: PhantomData,
|
||||||
})
|
})
|
||||||
@ -204,7 +204,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct AsyncHandleService<F, T, R>
|
pub struct AsyncHandlerService<F, T, R>
|
||||||
where
|
where
|
||||||
F: AsyncFactory<T, R>,
|
F: AsyncFactory<T, R>,
|
||||||
R: IntoFuture,
|
R: IntoFuture,
|
||||||
@ -215,7 +215,7 @@ where
|
|||||||
_t: PhantomData<(T, R)>,
|
_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
|
where
|
||||||
F: AsyncFactory<T, R>,
|
F: AsyncFactory<T, R>,
|
||||||
R: IntoFuture,
|
R: IntoFuture,
|
||||||
@ -224,14 +224,14 @@ where
|
|||||||
{
|
{
|
||||||
type Response = ServiceResponse;
|
type Response = ServiceResponse;
|
||||||
type Error = ();
|
type Error = ();
|
||||||
type Future = AsyncHandleServiceResponse<R::Future>;
|
type Future = AsyncHandlerServiceResponse<R::Future>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
fn call(&mut self, (param, req): (T, HttpRequest)) -> Self::Future {
|
||||||
AsyncHandleServiceResponse {
|
AsyncHandlerServiceResponse {
|
||||||
fut: self.hnd.call(param).into_future(),
|
fut: self.hnd.call(param).into_future(),
|
||||||
req: Some(req),
|
req: Some(req),
|
||||||
}
|
}
|
||||||
@ -239,12 +239,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct AsyncHandleServiceResponse<T> {
|
pub struct AsyncHandlerServiceResponse<T> {
|
||||||
fut: T,
|
fut: T,
|
||||||
req: Option<HttpRequest>,
|
req: Option<HttpRequest>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Future for AsyncHandleServiceResponse<T>
|
impl<T> Future for AsyncHandlerServiceResponse<T>
|
||||||
where
|
where
|
||||||
T: Future,
|
T: Future,
|
||||||
T::Item: Into<Response>,
|
T::Item: Into<Response>,
|
||||||
|
18
src/lib.rs
18
src/lib.rs
@ -18,6 +18,24 @@ mod service;
|
|||||||
mod state;
|
mod state;
|
||||||
pub mod test;
|
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
|
// re-export for convenience
|
||||||
pub use actix_http::Response as HttpResponse;
|
pub use actix_http::Response as HttpResponse;
|
||||||
pub use actix_http::{body, error, http, Error, HttpMessage, ResponseError, Result};
|
pub use actix_http::{body, error, http, Error, HttpMessage, ResponseError, Result};
|
||||||
|
11
src/route.rs
11
src/route.rs
@ -8,7 +8,7 @@ use futures::{Async, Future, IntoFuture, Poll};
|
|||||||
|
|
||||||
use crate::extract::{ConfigStorage, ExtractorConfig, FromRequest};
|
use crate::extract::{ConfigStorage, ExtractorConfig, FromRequest};
|
||||||
use crate::guard::{self, Guard};
|
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::responder::Responder;
|
||||||
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
||||||
use crate::HttpResponse;
|
use crate::HttpResponse;
|
||||||
@ -50,8 +50,9 @@ impl<P: 'static> Route<P> {
|
|||||||
let config_ref = Rc::new(RefCell::new(None));
|
let config_ref = Rc::new(RefCell::new(None));
|
||||||
Route {
|
Route {
|
||||||
service: Box::new(RouteNewService::new(
|
service: Box::new(RouteNewService::new(
|
||||||
Extract::new(config_ref.clone())
|
Extract::new(config_ref.clone()).and_then(
|
||||||
.and_then(Handle::new(HttpResponse::NotFound).map_err(|_| panic!())),
|
Handler::new(HttpResponse::NotFound).map_err(|_| panic!()),
|
||||||
|
),
|
||||||
)),
|
)),
|
||||||
guards: Rc::new(Vec::new()),
|
guards: Rc::new(Vec::new()),
|
||||||
config: ConfigStorage::default(),
|
config: ConfigStorage::default(),
|
||||||
@ -272,7 +273,7 @@ impl<P: 'static> Route<P> {
|
|||||||
T::Config::store_default(&mut self.config);
|
T::Config::store_default(&mut self.config);
|
||||||
self.service = Box::new(RouteNewService::new(
|
self.service = Box::new(RouteNewService::new(
|
||||||
Extract::new(self.config_ref.clone())
|
Extract::new(self.config_ref.clone())
|
||||||
.and_then(Handle::new(handler).map_err(|_| panic!())),
|
.and_then(Handler::new(handler).map_err(|_| panic!())),
|
||||||
));
|
));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -314,7 +315,7 @@ impl<P: 'static> Route<P> {
|
|||||||
{
|
{
|
||||||
self.service = Box::new(RouteNewService::new(
|
self.service = Box::new(RouteNewService::new(
|
||||||
Extract::new(self.config_ref.clone())
|
Extract::new(self.config_ref.clone())
|
||||||
.and_then(AsyncHandle::new(handler).map_err(|_| panic!())),
|
.and_then(AsyncHandler::new(handler).map_err(|_| panic!())),
|
||||||
));
|
));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user