mirror of
https://github.com/actix/actix-web.git
synced 2024-11-22 09:31:10 +00:00
added proc-macros for route registration
This commit is contained in:
parent
1151b5bf7c
commit
22708e78a9
9 changed files with 221 additions and 42 deletions
|
@ -29,6 +29,7 @@ members = [
|
|||
".",
|
||||
"actix-files",
|
||||
"actix-session",
|
||||
"actix-web-codegen",
|
||||
]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
|
@ -63,6 +64,7 @@ actix-codec = "0.1.0"
|
|||
#actix-service = "0.3.2"
|
||||
#actix-utils = "0.3.1"
|
||||
actix-rt = "0.2.0"
|
||||
actix-web-codegen = { path="actix-web-codegen" }
|
||||
|
||||
actix-service = { 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>>
|
||||
for Files<P, C>
|
||||
{
|
||||
impl<P, C: StaticFileConfig + 'static> NewService<ServiceRequest<P>> for Files<P, C> {
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Service = FilesService<P, C>;
|
||||
|
@ -730,8 +728,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_named_file_content_range_headers() {
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.service(Files::new("/test", ".").index_file("tests/test.binary")),
|
||||
App::new().service(Files::new("/test", ".").index_file("tests/test.binary")),
|
||||
);
|
||||
|
||||
// Valid range header
|
||||
|
@ -770,8 +767,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_named_file_content_length_headers() {
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||
App::new().service(Files::new("test", ".").index_file("tests/test.binary")),
|
||||
);
|
||||
|
||||
// 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 actix_web::{
|
||||
http::Method, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
|
||||
};
|
||||
use actix_web::macros::get;
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
|
||||
#[get("/resource1/index.html")]
|
||||
fn index(req: HttpRequest) -> &'static str {
|
||||
println!("REQ: {:?}", req);
|
||||
"Hello world!\r\n"
|
||||
|
@ -14,6 +14,7 @@ fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error =
|
|||
Ok("Hello world!\r\n")
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn no_params() -> &'static str {
|
||||
"Hello world!\r\n"
|
||||
}
|
||||
|
@ -27,7 +28,8 @@ fn main() -> std::io::Result<()> {
|
|||
App::new()
|
||||
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
|
||||
.middleware(middleware::Compress::default())
|
||||
.service(web::resource("/resource1/index.html").route(web::get().to(index)))
|
||||
.service(index)
|
||||
.service(no_params)
|
||||
.service(
|
||||
web::resource("/resource2/index.html")
|
||||
.middleware(
|
||||
|
@ -36,10 +38,9 @@ fn main() -> std::io::Result<()> {
|
|||
.default_resource(|r| {
|
||||
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("/").to(no_params))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.workers(1)
|
||||
|
|
|
@ -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>,
|
||||
|
|
18
src/lib.rs
18
src/lib.rs
|
@ -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};
|
||||
|
|
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::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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue