mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
extractors: done-ish.
This commit is contained in:
@ -1,18 +0,0 @@
|
||||
use actix_web::{web, HttpRequest, HttpResponse};
|
||||
|
||||
struct MyHandler {}
|
||||
struct MyInfo {}
|
||||
|
||||
// <custom-handler>
|
||||
impl<S> Handler<S> for MyHandler {
|
||||
type Result = HttpResponse;
|
||||
|
||||
/// Handle request
|
||||
fn handle(&self, req: &HttpRequest<S>) -> Self::Result {
|
||||
let params = web::Path::<(String, String)>::extract(req);
|
||||
let info = web::Json::<MyInfo>::extract(req);
|
||||
|
||||
HttpResponse::Ok().into()
|
||||
}
|
||||
}
|
||||
// </custom-handler>
|
@ -1,5 +1,5 @@
|
||||
// <form>
|
||||
use actix_web::{web, App, Result};
|
||||
use actix_web::{web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -16,5 +16,9 @@ fn index(form: web::Form<FormData>) -> Result<String> {
|
||||
// </form>
|
||||
|
||||
pub fn main() {
|
||||
App::new().route("", web::post().to(index));
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <json-one>
|
||||
use actix_web::{web, App, Result};
|
||||
use actix_web::{web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -11,8 +11,12 @@ struct Info {
|
||||
fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
// </json-one>
|
||||
|
||||
pub fn main() {
|
||||
App::new().route("/", web::get().to(index));
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </json-one>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <json-two>
|
||||
use actix_web::{error, web, App, FromRequest, HttpResponse, Responder};
|
||||
use actix_web::{error, web, App, FromRequest, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -13,22 +13,28 @@ fn index(info: web::Json<Info>) -> impl Responder {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
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()
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(web::post().to(index)),
|
||||
);
|
||||
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()
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </json-two>
|
||||
|
@ -8,6 +8,7 @@ pub mod json_one;
|
||||
pub mod json_two;
|
||||
pub mod multiple;
|
||||
pub mod path_one;
|
||||
pub mod path_three;
|
||||
pub mod path_two;
|
||||
pub mod query;
|
||||
|
||||
@ -17,13 +18,13 @@ struct MyInfo {
|
||||
id: u32,
|
||||
}
|
||||
|
||||
// <main>
|
||||
// Option 1: passed as a parameter to a handler function
|
||||
// <option-one>
|
||||
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 2: accessed by calling extract() on the Extractor
|
||||
// <option-two>
|
||||
fn extract(req: HttpRequest) -> impl Responder {
|
||||
let params = web::Path::<(String, String)>::extract(&req).unwrap();
|
||||
|
||||
@ -33,7 +34,7 @@ fn extract(req: HttpRequest) -> impl Responder {
|
||||
|
||||
format!("{} {} {} {}", params.0, params.1, info.username, info.id)
|
||||
}
|
||||
// </main>
|
||||
// </option-two>
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(|| {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <multi>
|
||||
use actix_web::{web, App};
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -7,14 +7,23 @@ struct Info {
|
||||
username: String,
|
||||
}
|
||||
|
||||
fn index((_path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String {
|
||||
format!("Welcome {}!", query.username)
|
||||
fn index((path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String {
|
||||
format!(
|
||||
"Welcome {}, friend {}, useri {}!",
|
||||
query.username, path.1, path.0
|
||||
)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
);
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </multi>
|
||||
|
@ -1,17 +1,23 @@
|
||||
// <path-one>
|
||||
use actix_web::{web, App, Result};
|
||||
use actix_web::{web, App, HttpServer, 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> {
|
||||
Ok(format!("Welcome {}! {}", info.1, info.0))
|
||||
Ok(format!("Welcome {}, userid {}!", info.1, info.0))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
);
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </path-one>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <path-two>
|
||||
use actix_web::{web, App, Result};
|
||||
use actix_web::{web, App, HttpServer, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -14,9 +14,15 @@ fn index(info: web::Path<Info>) -> Result<String> {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
);
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/users/{userid}/{friend}", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </path-two>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <query>
|
||||
use actix_web::{web, App};
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -11,8 +11,12 @@ struct Info {
|
||||
fn index(info: web::Query<Info>) -> String {
|
||||
format!("Welcome {}!", info.username)
|
||||
}
|
||||
// </query>
|
||||
|
||||
pub fn main() {
|
||||
App::new().route("/", web::get().to(index));
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </query>
|
||||
|
Reference in New Issue
Block a user