diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 1052c82a7..26b422d77 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -35,7 +35,8 @@ pub fn get(args: TokenStream, input: TokenStream) -> TokenStream { #ast actix_web::dev::HttpServiceFactory::register( actix_web::Resource::new(#path) - .route(actix_web::web::get().to(#name)), config); + .guard(actix_web::guard::Get()) + .to(#name), config); } } }) @@ -71,7 +72,8 @@ pub fn post(args: TokenStream, input: TokenStream) -> TokenStream { #ast actix_web::dev::HttpServiceFactory::register( actix_web::Resource::new(#path) - .route(actix_web::web::post().to(#name)), config); + .guard(actix_web::guard::Post()) + .to(#name), config); } } }) @@ -107,7 +109,8 @@ pub fn put(args: TokenStream, input: TokenStream) -> TokenStream { #ast actix_web::dev::HttpServiceFactory::register( actix_web::Resource::new(#path) - .route(actix_web::web::put().to(#name)), config); + .guard(actix_web::guard::Put()) + .to(#name), config); } } }) diff --git a/examples/basic.rs b/examples/basic.rs index 39633f523..3f832780a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -3,10 +3,10 @@ use futures::IntoFuture; 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 { +#[get("/resource1/{name}/index.html")] +fn index(req: HttpRequest, name: web::Path) -> String { println!("REQ: {:?}", req); - "Hello world!\r\n" + format!("Hello: {}!\r\n", name) } fn index_async(req: HttpRequest) -> impl IntoFuture { @@ -20,14 +20,14 @@ fn no_params() -> &'static str { } fn main() -> std::io::Result<()> { - ::std::env::set_var("RUST_LOG", "actix_server=info,actix_web2=info"); + std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); env_logger::init(); - let sys = actix_rt::System::new("hello-world"); HttpServer::new(|| { App::new() .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2")) .middleware(middleware::Compress::default()) + .middleware(middleware::Logger::default()) .service(index) .service(no_params) .service( @@ -44,7 +44,5 @@ fn main() -> std::io::Result<()> { }) .bind("127.0.0.1:8080")? .workers(1) - .start(); - - sys.run() + .run() } diff --git a/src/app.rs b/src/app.rs index f62c064a7..1cff1788e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -75,13 +75,13 @@ where /// /// ```rust /// use std::cell::Cell; - /// use actix_web::{web, State, App}; + /// use actix_web::{web, App}; /// /// struct MyState { /// counter: Cell, /// } /// - /// fn index(state: State) { + /// fn index(state: web::State) { /// state.counter.set(state.counter.get() + 1); /// } /// @@ -785,7 +785,7 @@ mod tests { use super::*; use crate::http::{Method, StatusCode}; use crate::test::{block_on, init_service, TestRequest}; - use crate::{web, HttpResponse, State}; + use crate::{web, HttpResponse}; #[test] fn test_default_resource() { @@ -828,21 +828,19 @@ mod tests { #[test] fn test_state() { - let mut srv = init_service( - App::new() - .state(10usize) - .service(web::resource("/").to(|_: State| HttpResponse::Ok())), - ); + let mut srv = + init_service(App::new().state(10usize).service( + web::resource("/").to(|_: web::State| HttpResponse::Ok()), + )); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - let mut srv = init_service( - App::new() - .state(10u32) - .service(web::resource("/").to(|_: State| HttpResponse::Ok())), - ); + let mut srv = + init_service(App::new().state(10u32).service( + web::resource("/").to(|_: web::State| HttpResponse::Ok()), + )); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); @@ -850,20 +848,18 @@ mod tests { #[test] fn test_state_factory() { - let mut srv = init_service( - App::new() - .state_factory(|| Ok::<_, ()>(10usize)) - .service(web::resource("/").to(|_: State| HttpResponse::Ok())), - ); + let mut srv = + init_service(App::new().state_factory(|| Ok::<_, ()>(10usize)).service( + web::resource("/").to(|_: web::State| HttpResponse::Ok()), + )); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - let mut srv = init_service( - App::new() - .state_factory(|| Ok::<_, ()>(10u32)) - .service(web::resource("/").to(|_: State| HttpResponse::Ok())), - ); + let mut srv = + init_service(App::new().state_factory(|| Ok::<_, ()>(10u32)).service( + web::resource("/").to(|_: web::State| HttpResponse::Ok()), + )); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); diff --git a/src/extract.rs b/src/extract.rs index 0b212aba5..6c838901f 100644 --- a/src/extract.rs +++ b/src/extract.rs @@ -554,8 +554,8 @@ impl Default for FormConfig { /// name: String, /// } /// -/// fn index(req: HttpRequest) -> Result> { -/// Ok(Json(MyObj { +/// fn index(req: HttpRequest) -> Result> { +/// Ok(web::Json(MyObj { /// name: req.match_info().get("name").unwrap().to_string(), /// })) /// } @@ -679,7 +679,7 @@ where /// /// ```rust /// #[macro_use] extern crate serde_derive; -/// use actix_web::{error, extract, web, App, HttpResponse, Json}; +/// use actix_web::{error, extract, web, App, HttpResponse}; /// /// #[derive(Deserialize)] /// struct Info { @@ -687,7 +687,7 @@ where /// } /// /// /// deserialize `Info` from request's body, max payload size is 4kb -/// fn index(info: Json) -> String { +/// fn index(info: web::Json) -> String { /// format!("Welcome {}!", info.username) /// } /// diff --git a/src/lib.rs b/src/lib.rs index dd60c7b84..35a88b981 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ pub use actix_http::Response as HttpResponse; pub use actix_http::{body, error, http, Error, HttpMessage, ResponseError, Result}; pub use crate::app::App; -pub use crate::extract::{FromRequest, Json}; +pub use crate::extract::FromRequest; pub use crate::request::HttpRequest; pub use crate::resource::Resource; pub use crate::responder::{Either, Responder}; @@ -49,7 +49,6 @@ pub use crate::route::Route; pub use crate::scope::Scope; pub use crate::server::HttpServer; pub use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse}; -pub use crate::state::State; pub mod dev { //! The `actix-web` prelude for library developers @@ -93,6 +92,9 @@ pub mod web { use crate::route::Route; use crate::scope::Scope; + pub use crate::extract::{Json, Path, Query}; + pub use crate::state::State; + /// Create resource for a specific path. /// /// Resources may have variable path segments. For example, a diff --git a/src/route.rs b/src/route.rs index 9538dfd2c..45bc65344 100644 --- a/src/route.rs +++ b/src/route.rs @@ -245,7 +245,7 @@ impl Route

{ /// ```rust /// # use std::collections::HashMap; /// # use serde_derive::Deserialize; - /// use actix_web::{web, App, Json, extract::Path, extract::Query}; + /// use actix_web::{web, App}; /// /// #[derive(Deserialize)] /// struct Info { @@ -253,7 +253,7 @@ impl Route

{ /// } /// /// /// extract path info using serde - /// fn index(path: Path, query: Query>, body: Json) -> String { + /// fn index(path: web::Path, query: web::Query>, body: web::Json) -> String { /// format!("Welcome {}!", path.username) /// } ///