mirror of
https://github.com/actix/actix-website
synced 2025-02-02 12:19:04 +01:00
40 lines
1.0 KiB
Rust
40 lines
1.0 KiB
Rust
#![allow(dead_code)]
|
|
|
|
// <json-two>
|
|
use actix_web::{error, web, App, HttpResponse, HttpServer, Responder};
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Deserialize)]
|
|
struct Info {
|
|
username: String,
|
|
}
|
|
|
|
/// deserialize `Info` from request's body, max payload size is 4kb
|
|
async fn index(info: web::Json<Info>) -> impl Responder {
|
|
format!("Welcome {}!", info.username)
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
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(json_config)
|
|
.route(web::post().to(index)),
|
|
)
|
|
})
|
|
.bind(("127.0.0.1", 8080))?
|
|
.run()
|
|
.await
|
|
}
|
|
// </json-two>
|