mirror of
https://github.com/actix/actix-web.git
synced 2024-11-25 11:01:14 +00:00
reexpost extractors in web module
This commit is contained in:
parent
22708e78a9
commit
ceb6d45bf2
6 changed files with 41 additions and 42 deletions
|
@ -35,7 +35,8 @@ pub fn get(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
#ast
|
#ast
|
||||||
actix_web::dev::HttpServiceFactory::register(
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
actix_web::Resource::new(#path)
|
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
|
#ast
|
||||||
actix_web::dev::HttpServiceFactory::register(
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
actix_web::Resource::new(#path)
|
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
|
#ast
|
||||||
actix_web::dev::HttpServiceFactory::register(
|
actix_web::dev::HttpServiceFactory::register(
|
||||||
actix_web::Resource::new(#path)
|
actix_web::Resource::new(#path)
|
||||||
.route(actix_web::web::put().to(#name)), config);
|
.guard(actix_web::guard::Put())
|
||||||
|
.to(#name), config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -3,10 +3,10 @@ use futures::IntoFuture;
|
||||||
use actix_web::macros::get;
|
use actix_web::macros::get;
|
||||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||||
|
|
||||||
#[get("/resource1/index.html")]
|
#[get("/resource1/{name}/index.html")]
|
||||||
fn index(req: HttpRequest) -> &'static str {
|
fn index(req: HttpRequest, name: web::Path<String>) -> String {
|
||||||
println!("REQ: {:?}", req);
|
println!("REQ: {:?}", req);
|
||||||
"Hello world!\r\n"
|
format!("Hello: {}!\r\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error = Error> {
|
fn index_async(req: HttpRequest) -> impl IntoFuture<Item = &'static str, Error = Error> {
|
||||||
|
@ -20,14 +20,14 @@ fn no_params() -> &'static str {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
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();
|
env_logger::init();
|
||||||
let sys = actix_rt::System::new("hello-world");
|
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
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())
|
||||||
|
.middleware(middleware::Logger::default())
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(no_params)
|
.service(no_params)
|
||||||
.service(
|
.service(
|
||||||
|
@ -44,7 +44,5 @@ fn main() -> std::io::Result<()> {
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.workers(1)
|
.workers(1)
|
||||||
.start();
|
.run()
|
||||||
|
|
||||||
sys.run()
|
|
||||||
}
|
}
|
||||||
|
|
42
src/app.rs
42
src/app.rs
|
@ -75,13 +75,13 @@ where
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use std::cell::Cell;
|
/// use std::cell::Cell;
|
||||||
/// use actix_web::{web, State, App};
|
/// use actix_web::{web, App};
|
||||||
///
|
///
|
||||||
/// struct MyState {
|
/// struct MyState {
|
||||||
/// counter: Cell<usize>,
|
/// counter: Cell<usize>,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn index(state: State<MyState>) {
|
/// fn index(state: web::State<MyState>) {
|
||||||
/// state.counter.set(state.counter.get() + 1);
|
/// state.counter.set(state.counter.get() + 1);
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
@ -785,7 +785,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::http::{Method, StatusCode};
|
use crate::http::{Method, StatusCode};
|
||||||
use crate::test::{block_on, init_service, TestRequest};
|
use crate::test::{block_on, init_service, TestRequest};
|
||||||
use crate::{web, HttpResponse, State};
|
use crate::{web, HttpResponse};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_resource() {
|
fn test_default_resource() {
|
||||||
|
@ -828,21 +828,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_state() {
|
fn test_state() {
|
||||||
let mut srv = init_service(
|
let mut srv =
|
||||||
App::new()
|
init_service(App::new().state(10usize).service(
|
||||||
.state(10usize)
|
web::resource("/").to(|_: web::State<usize>| HttpResponse::Ok()),
|
||||||
.service(web::resource("/").to(|_: State<usize>| HttpResponse::Ok())),
|
));
|
||||||
);
|
|
||||||
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let mut srv =
|
||||||
App::new()
|
init_service(App::new().state(10u32).service(
|
||||||
.state(10u32)
|
web::resource("/").to(|_: web::State<usize>| HttpResponse::Ok()),
|
||||||
.service(web::resource("/").to(|_: State<usize>| HttpResponse::Ok())),
|
));
|
||||||
);
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
@ -850,20 +848,18 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_state_factory() {
|
fn test_state_factory() {
|
||||||
let mut srv = init_service(
|
let mut srv =
|
||||||
App::new()
|
init_service(App::new().state_factory(|| Ok::<_, ()>(10usize)).service(
|
||||||
.state_factory(|| Ok::<_, ()>(10usize))
|
web::resource("/").to(|_: web::State<usize>| HttpResponse::Ok()),
|
||||||
.service(web::resource("/").to(|_: State<usize>| HttpResponse::Ok())),
|
));
|
||||||
);
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let mut srv = init_service(
|
let mut srv =
|
||||||
App::new()
|
init_service(App::new().state_factory(|| Ok::<_, ()>(10u32)).service(
|
||||||
.state_factory(|| Ok::<_, ()>(10u32))
|
web::resource("/").to(|_: web::State<usize>| HttpResponse::Ok()),
|
||||||
.service(web::resource("/").to(|_: State<usize>| HttpResponse::Ok())),
|
));
|
||||||
);
|
|
||||||
let req = TestRequest::default().to_request();
|
let req = TestRequest::default().to_request();
|
||||||
let resp = block_on(srv.call(req)).unwrap();
|
let resp = block_on(srv.call(req)).unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
|
@ -554,8 +554,8 @@ impl Default for FormConfig {
|
||||||
/// name: String,
|
/// name: String,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
/// fn index(req: HttpRequest) -> Result<web::Json<MyObj>> {
|
||||||
/// Ok(Json(MyObj {
|
/// Ok(web::Json(MyObj {
|
||||||
/// name: req.match_info().get("name").unwrap().to_string(),
|
/// name: req.match_info().get("name").unwrap().to_string(),
|
||||||
/// }))
|
/// }))
|
||||||
/// }
|
/// }
|
||||||
|
@ -679,7 +679,7 @@ where
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// #[macro_use] extern crate serde_derive;
|
/// #[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)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct Info {
|
/// struct Info {
|
||||||
|
@ -687,7 +687,7 @@ where
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// deserialize `Info` from request's body, max payload size is 4kb
|
/// /// deserialize `Info` from request's body, max payload size is 4kb
|
||||||
/// fn index(info: Json<Info>) -> String {
|
/// fn index(info: web::Json<Info>) -> String {
|
||||||
/// format!("Welcome {}!", info.username)
|
/// format!("Welcome {}!", info.username)
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
|
|
@ -41,7 +41,7 @@ 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};
|
||||||
|
|
||||||
pub use crate::app::App;
|
pub use crate::app::App;
|
||||||
pub use crate::extract::{FromRequest, Json};
|
pub use crate::extract::FromRequest;
|
||||||
pub use crate::request::HttpRequest;
|
pub use crate::request::HttpRequest;
|
||||||
pub use crate::resource::Resource;
|
pub use crate::resource::Resource;
|
||||||
pub use crate::responder::{Either, Responder};
|
pub use crate::responder::{Either, Responder};
|
||||||
|
@ -49,7 +49,6 @@ pub use crate::route::Route;
|
||||||
pub use crate::scope::Scope;
|
pub use crate::scope::Scope;
|
||||||
pub use crate::server::HttpServer;
|
pub use crate::server::HttpServer;
|
||||||
pub use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
pub use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
||||||
pub use crate::state::State;
|
|
||||||
|
|
||||||
pub mod dev {
|
pub mod dev {
|
||||||
//! The `actix-web` prelude for library developers
|
//! The `actix-web` prelude for library developers
|
||||||
|
@ -93,6 +92,9 @@ pub mod web {
|
||||||
use crate::route::Route;
|
use crate::route::Route;
|
||||||
use crate::scope::Scope;
|
use crate::scope::Scope;
|
||||||
|
|
||||||
|
pub use crate::extract::{Json, Path, Query};
|
||||||
|
pub use crate::state::State;
|
||||||
|
|
||||||
/// Create resource for a specific path.
|
/// Create resource for a specific path.
|
||||||
///
|
///
|
||||||
/// Resources may have variable path segments. For example, a
|
/// Resources may have variable path segments. For example, a
|
||||||
|
|
|
@ -245,7 +245,7 @@ impl<P: 'static> Route<P> {
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use std::collections::HashMap;
|
/// # use std::collections::HashMap;
|
||||||
/// # use serde_derive::Deserialize;
|
/// # use serde_derive::Deserialize;
|
||||||
/// use actix_web::{web, App, Json, extract::Path, extract::Query};
|
/// use actix_web::{web, App};
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct Info {
|
/// struct Info {
|
||||||
|
@ -253,7 +253,7 @@ impl<P: 'static> Route<P> {
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// /// extract path info using serde
|
/// /// extract path info using serde
|
||||||
/// fn index(path: Path<Info>, query: Query<HashMap<String, String>>, body: Json<Info>) -> String {
|
/// fn index(path: web::Path<Info>, query: web::Query<HashMap<String, String>>, body: web::Json<Info>) -> String {
|
||||||
/// format!("Welcome {}!", path.username)
|
/// format!("Welcome {}!", path.username)
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
|
Loading…
Reference in a new issue