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
|
2019-12-28 16:26:17 +01:00
|
|
|
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]
|
2019-12-28 16:26:17 +01:00
|
|
|
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()
|
2019-12-28 16:26:17 +01:00
|
|
|
.await
|
2019-06-16 23:17:17 -04:00
|
|
|
}
|
|
|
|
// </json-two>
|