mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
v3 (#188)
This commit is contained in:
@ -4,7 +4,6 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <form>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{post, web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -10,17 +10,16 @@ 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
|
||||
#[post("/")]
|
||||
async fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", form.username))
|
||||
}
|
||||
// </form>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::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")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <json-one>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -8,17 +8,16 @@ struct Info {
|
||||
}
|
||||
|
||||
/// deserialize `Info` from request's body
|
||||
#[get("/")]
|
||||
async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
// </json-one>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::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")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
// <json-two>
|
||||
use actix_web::{error, web, FromRequest, HttpResponse, Responder};
|
||||
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -12,28 +14,24 @@ async fn index(info: web::Json<Info>) -> impl Responder {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
let json_config = web::JsonConfig::default()
|
||||
.limit(4096)
|
||||
.error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
error::InternalError::from_response(err, HttpResponse::Conflict().finish()).into()
|
||||
});
|
||||
|
||||
App::new().service(
|
||||
web::resource("/")
|
||||
// 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()
|
||||
})
|
||||
}))
|
||||
.app_data(json_config)
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -18,17 +18,18 @@ struct MyInfo {
|
||||
}
|
||||
|
||||
// <option-one>
|
||||
async 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 {
|
||||
let path = path.into_inner();
|
||||
format!("{} {} {} {}", path.0, path.1, json.id, json.username)
|
||||
}
|
||||
// </option-one>
|
||||
|
||||
// <option-two>
|
||||
async fn extract(req: HttpRequest) -> impl Responder {
|
||||
let params = web::Path::<(String, String)>::extract(&req).await.unwrap();
|
||||
let params = web::Path::<(String, String)>::extract(&req)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_inner();
|
||||
|
||||
let info = web::Json::<MyInfo>::extract(&req)
|
||||
.await
|
||||
@ -38,14 +39,14 @@ async fn extract(req: HttpRequest) -> impl Responder {
|
||||
}
|
||||
// </option-two>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::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")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <multi>
|
||||
use actix_web::web;
|
||||
use actix_web::{get, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -7,27 +7,24 @@ struct Info {
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(
|
||||
(path, query): (web::Path<(u32, String)>, web::Query<Info>),
|
||||
web::Path((user_id, friend)): web::Path<(u32, String)>,
|
||||
query: web::Query<Info>,
|
||||
) -> String {
|
||||
format!(
|
||||
"Welcome {}, friend {}, userid {}!",
|
||||
query.username, path.1, path.0
|
||||
"Welcome {}, friend {}, user_id {}!",
|
||||
query.username, friend, user_id
|
||||
)
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </multi>
|
||||
|
@ -1,25 +1,21 @@
|
||||
// <path-one>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, Result};
|
||||
|
||||
/// extract path info from "/users/{userid}/{friend}" url
|
||||
/// {userid} - - deserializes to a u32
|
||||
/// extract path info from "/users/{user_id}/{friend}" url
|
||||
/// {user_id} - deserializes to a u32
|
||||
/// {friend} - deserializes to a String
|
||||
async fn index(info: web::Path<(u32, String)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.1, info.0))
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(web::Path((user_id, friend)): web::Path<(u32, String)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, user_id {}!", friend, user_id))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-one>
|
||||
|
@ -1,26 +1,21 @@
|
||||
use actix_web::{web, HttpRequest, Result};
|
||||
use actix_web::{get, HttpRequest, Result};
|
||||
|
||||
// <path-three>
|
||||
#[get("/users/{userid}/{friend}")] // <- define path parameters
|
||||
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))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-three>
|
||||
|
@ -1,30 +1,29 @@
|
||||
// <path-two>
|
||||
use actix_web::{web, Result};
|
||||
use actix_web::{get, web, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Info {
|
||||
userid: u32,
|
||||
user_id: u32,
|
||||
friend: String,
|
||||
}
|
||||
|
||||
/// extract path info using serde
|
||||
#[get("/users/{user_id}/{friend}")] // <- define path parameters
|
||||
async fn index(info: web::Path<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}, userid {}!", info.friend, info.userid))
|
||||
Ok(format!(
|
||||
"Welcome {}, user_id {}!",
|
||||
info.friend, info.user_id
|
||||
))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </path-two>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <query>
|
||||
use actix_web::web;
|
||||
use actix_web::{get, web, App, HttpServer};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -8,17 +8,16 @@ struct Info {
|
||||
}
|
||||
|
||||
// this handler get called only if the request's query contains `username` field
|
||||
#[get("/")]
|
||||
async fn index(info: web::Query<Info>) -> String {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
// </query>
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::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")?
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user