mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +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:
@ -4,7 +4,8 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
serde = "1.0"
|
||||
futures = "0.1"
|
||||
futures = "0.3"
|
||||
serde_json = "1.0"
|
||||
|
@ -10,17 +10,17 @@ struct FormData {
|
||||
/// extract form data using serde
|
||||
/// this handler gets called only if the content type is *x-www-form-urlencoded*
|
||||
/// and the content of the request could be deserialized to a `FormData` struct
|
||||
fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
async fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", form.username))
|
||||
}
|
||||
// </form>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -8,17 +8,17 @@ struct Info {
|
||||
}
|
||||
|
||||
/// deserialize `Info` from request's body
|
||||
fn index(info: web::Json<Info>) -> Result<String> {
|
||||
async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
// </json-one>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -8,11 +8,12 @@ struct Info {
|
||||
}
|
||||
|
||||
/// deserialize `Info` from request's body, max payload size is 4kb
|
||||
fn index(info: web::Json<Info>) -> impl Responder {
|
||||
async fn index(info: web::Json<Info>) -> impl Responder {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -34,9 +35,8 @@ pub fn main() {
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </json-two>
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -7,14 +7,15 @@ struct Info {
|
||||
username: String,
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -23,9 +24,8 @@ pub fn main() {
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </multi>
|
||||
|
@ -4,11 +4,12 @@ use actix_web::{web, Result};
|
||||
/// extract path info from "/users/{userid}/{friend}" url
|
||||
/// {userid} - - deserializes to a u32
|
||||
/// {friend} - deserializes to a String
|
||||
fn index(info: web::Path<(u32, String)>) -> Result<String> {
|
||||
async fn index(info: web::Path<(u32, String)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.1, info.0))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -17,9 +18,8 @@ pub fn main() {
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </path-one>
|
||||
|
@ -1,14 +1,15 @@
|
||||
use actix_web::{web, HttpRequest, Result};
|
||||
|
||||
// <path-three>
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
async fn index(req: HttpRequest) -> Result<String> {
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -17,9 +18,8 @@ pub fn main() {
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </path-three>
|
||||
|
@ -9,11 +9,12 @@ struct Info {
|
||||
}
|
||||
|
||||
/// extract path info using serde
|
||||
fn index(info: web::Path<Info>) -> Result<String> {
|
||||
async fn index(info: web::Path<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.friend, info.userid))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
@ -22,9 +23,8 @@ pub fn main() {
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </path-two>
|
||||
|
@ -8,17 +8,17 @@ struct Info {
|
||||
}
|
||||
|
||||
// this handler get called only if the request's query contains `username` field
|
||||
fn index(info: web::Query<Info>) -> String {
|
||||
async fn index(info: web::Query<Info>) -> String {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
// </query>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user