1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 15:39:02 +02:00

update extractors

This commit is contained in:
Nikolay Kim
2020-01-02 18:33:33 +06:00
parent 4046b0b697
commit b8e2a2310c
8 changed files with 42 additions and 58 deletions

View File

@ -19,19 +19,17 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
web::resource("/")
.data(
// change json extractor configuration
web::Json::<Info>::configure(|cfg| {
cfg.limit(4096).error_handler(|err, _req| {
// <- create custom error response
error::InternalError::from_response(
err,
HttpResponse::Conflict().finish(),
)
.into()
})
}),
)
// change json extractor configuration
.app_data(web::Json::<Info>::configure(|cfg| {
cfg.limit(4096).error_handler(|err, _req| {
// create custom error response
error::InternalError::from_response(
err,
HttpResponse::Conflict().finish(),
)
.into()
})
}))
.route(web::post().to(index)),
)
})

View File

@ -7,7 +7,9 @@ struct Info {
username: String,
}
async fn index((path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String {
async fn index(
(path, query): (web::Path<(u32, String)>, web::Query<Info>),
) -> String {
format!(
"Welcome {}, friend {}, userid {}!",
query.username, path.1, path.0

View File

@ -2,7 +2,8 @@ use actix_web::{web, HttpRequest, Result};
// <path-three>
async fn index(req: HttpRequest) -> Result<String> {
let name: String = req.match_info().get("friend").unwrap().parse().unwrap();
let name: String =
req.match_info().get("friend").unwrap().parse().unwrap();
let userid: i32 = req.match_info().query("userid").parse().unwrap();
Ok(format!("Welcome {}, userid {}!", name, userid))

View File

@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.data(data.clone())
.app_data(data.clone())
.route("/", web::to(show_count))
.route("/add", web::to(add_one))
})

View File

@ -29,7 +29,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.data(data.clone())
.app_data(data.clone())
.route("/", web::to(show_count))
.route("/add", web::to(add_one))
})

View File

@ -1,23 +1,22 @@
// // <chunked>
// use actix_web::{web, HttpRequest, HttpResponse};
// use bytes::Bytes;
// use futures::stream::once;
// <chunked>
use actix_web::{web, HttpRequest, HttpResponse};
use bytes::Bytes;
use futures::future::ok;
use futures::stream::once;
// fn index(req: HttpRequest) -> HttpResponse {
// HttpResponse::Ok()
// .chunked()
// .body(Body::Streaming(Box::new(once(Ok(Bytes::from_static(
// b"data",
// ))))))
// }
// // </chunked>
async fn index(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
.chunked()
.streaming(once(ok::<_, Error>(Bytes::from_static(b"data"))))
}
// </chunked>
// pub fn main() {
// use actix_web::{web, App, HttpServer};
pub fn main() {
use actix_web::{web, App, HttpServer};
// HttpServer::new(|| App::new().route("/", web::get().to(index)))
// .bind("127.0.0.1:8088")
// .unwrap()
// .run()
// .unwrap();
// }
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}