mirror of
https://github.com/actix/actix-website
synced 2025-07-01 01:04:27 +02:00
Update examples/{extractors, errors} to futures 0.3 and actix-web 2.0 (#130)
* Update to futures 0.3 * update examples/errors to actix-web 2.0
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
use actix_web::{web, App, FromRequest, HttpRequest, HttpServer, Responder};
|
||||
use futures::future::Future;
|
||||
use serde::Deserialize;
|
||||
|
||||
// pub mod custom_handler;
|
||||
@ -19,31 +18,34 @@ struct MyInfo {
|
||||
}
|
||||
|
||||
// <option-one>
|
||||
fn index(path: web::Path<(String, String)>, json: web::Json<MyInfo>) -> impl Responder {
|
||||
async fn index(
|
||||
path: web::Path<(String, String)>,
|
||||
json: web::Json<MyInfo>,
|
||||
) -> impl Responder {
|
||||
format!("{} {} {} {}", path.0, path.1, json.id, json.username)
|
||||
}
|
||||
// </option-one>
|
||||
|
||||
// <option-two>
|
||||
fn extract(req: HttpRequest) -> impl Responder {
|
||||
let params = web::Path::<(String, String)>::extract(&req).unwrap();
|
||||
async fn extract(req: HttpRequest) -> impl Responder {
|
||||
let params = web::Path::<(String, String)>::extract(&req).await.unwrap();
|
||||
|
||||
let info = web::Json::<MyInfo>::extract(&req)
|
||||
.wait()
|
||||
.await
|
||||
.expect("Err with reading json.");
|
||||
|
||||
format!("{} {} {} {}", params.0, params.1, info.username, info.id)
|
||||
}
|
||||
// </option-two>
|
||||
|
||||
fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/{name}/{id}", web::post().to(index))
|
||||
.route("/{name}/{id}/extract", web::post().to(extract))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user