1
0
mirror of https://github.com/actix/actix-website synced 2025-02-02 12:19:04 +01:00

39 lines
1018 B
Rust
Raw Normal View History

2020-09-12 16:21:54 +01:00
#![allow(dead_code)]
2019-06-16 23:17:17 -04:00
// <json-two>
2020-11-27 01:10:05 +00:00
use actix_web::{error, web, App, HttpResponse, HttpServer, Responder};
2019-06-16 23:17:17 -04:00
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 {
2019-06-16 23:17:17 -04:00
format!("Welcome {}!", info.username)
}
2020-09-12 16:21:54 +01:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2019-06-20 04:20:12 -04:00
HttpServer::new(|| {
2020-09-12 16:21:54 +01:00
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()
});
2019-06-20 04:20:12 -04:00
App::new().service(
web::resource("/")
2020-01-02 18:33:33 +06:00
// change json extractor configuration
2020-09-12 16:21:54 +01:00
.app_data(json_config)
2019-06-20 04:20:12 -04:00
.route(web::post().to(index)),
)
})
2020-09-12 16:21:54 +01:00
.bind("127.0.0.1:8080")?
2019-06-20 04:20:12 -04:00
.run()
.await
2019-06-16 23:17:17 -04:00
}
// </json-two>