mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
reexpost extractors in web module
This commit is contained in:
parent
22708e78a9
commit
ceb6d45bf2
@ -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);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -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>) -> String {
|
||||
println!("REQ: {:?}", req);
|
||||
"Hello world!\r\n"
|
||||
format!("Hello: {}!\r\n", name)
|
||||
}
|
||||
|
||||
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<()> {
|
||||
::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()
|
||||
}
|
||||
|
42
src/app.rs
42
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<usize>,
|
||||
/// }
|
||||
///
|
||||
/// fn index(state: State<MyState>) {
|
||||
/// fn index(state: web::State<MyState>) {
|
||||
/// 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<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
let mut srv =
|
||||
init_service(App::new().state(10usize).service(
|
||||
web::resource("/").to(|_: web::State<usize>| 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<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
let mut srv =
|
||||
init_service(App::new().state(10u32).service(
|
||||
web::resource("/").to(|_: web::State<usize>| 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<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
let mut srv =
|
||||
init_service(App::new().state_factory(|| Ok::<_, ()>(10usize)).service(
|
||||
web::resource("/").to(|_: web::State<usize>| 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<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
let mut srv =
|
||||
init_service(App::new().state_factory(|| Ok::<_, ()>(10u32)).service(
|
||||
web::resource("/").to(|_: web::State<usize>| HttpResponse::Ok()),
|
||||
));
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||
|
@ -554,8 +554,8 @@ impl Default for FormConfig {
|
||||
/// name: String,
|
||||
/// }
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
||||
/// Ok(Json(MyObj {
|
||||
/// fn index(req: HttpRequest) -> Result<web::Json<MyObj>> {
|
||||
/// 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<Info>) -> String {
|
||||
/// fn index(info: web::Json<Info>) -> String {
|
||||
/// 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 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
|
||||
|
@ -245,7 +245,7 @@ impl<P: 'static> Route<P> {
|
||||
/// ```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<P: 'static> Route<P> {
|
||||
/// }
|
||||
///
|
||||
/// /// 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)
|
||||
/// }
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user