2019-06-16 23:17:17 -04:00
|
|
|
use actix_web::{web, App, FromRequest, HttpRequest, HttpServer, Responder};
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2019-06-19 00:20:50 -04:00
|
|
|
// pub mod custom_handler;
|
|
|
|
pub mod form;
|
|
|
|
pub mod json_one;
|
|
|
|
pub mod json_two;
|
|
|
|
pub mod multiple;
|
|
|
|
pub mod path_one;
|
2019-06-20 04:20:12 -04:00
|
|
|
pub mod path_three;
|
2019-06-19 00:20:50 -04:00
|
|
|
pub mod path_two;
|
|
|
|
pub mod query;
|
2019-06-16 23:17:17 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct MyInfo {
|
|
|
|
username: String,
|
|
|
|
id: u32,
|
|
|
|
}
|
|
|
|
|
2019-06-20 04:20:12 -04:00
|
|
|
// <option-one>
|
2020-09-12 16:21:54 +01:00
|
|
|
async fn index(path: web::Path<(String, String)>, json: web::Json<MyInfo>) -> impl Responder {
|
|
|
|
let path = path.into_inner();
|
2019-06-16 23:17:17 -04:00
|
|
|
format!("{} {} {} {}", path.0, path.1, json.id, json.username)
|
|
|
|
}
|
2019-06-20 04:20:12 -04:00
|
|
|
// </option-one>
|
2019-06-16 23:17:17 -04:00
|
|
|
|
2019-06-20 04:20:12 -04:00
|
|
|
// <option-two>
|
2019-12-28 16:26:17 +01:00
|
|
|
async fn extract(req: HttpRequest) -> impl Responder {
|
2020-09-12 16:21:54 +01:00
|
|
|
let params = web::Path::<(String, String)>::extract(&req)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.into_inner();
|
2019-06-16 23:17:17 -04:00
|
|
|
|
|
|
|
let info = web::Json::<MyInfo>::extract(&req)
|
2019-12-28 16:26:17 +01:00
|
|
|
.await
|
2019-06-16 23:17:17 -04:00
|
|
|
.expect("Err with reading json.");
|
|
|
|
|
|
|
|
format!("{} {} {} {}", params.0, params.1, info.username, info.id)
|
|
|
|
}
|
2019-06-20 04:20:12 -04:00
|
|
|
// </option-two>
|
2019-06-16 23:17:17 -04:00
|
|
|
|
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-16 23:17:17 -04:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.route("/{name}/{id}", web::post().to(index))
|
|
|
|
.route("/{name}/{id}/extract", web::post().to(extract))
|
|
|
|
})
|
2022-02-26 03:56:24 +00:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2019-06-16 23:17:17 -04:00
|
|
|
.run()
|
2019-12-28 16:26:17 +01:00
|
|
|
.await
|
2019-06-16 23:17:17 -04:00
|
|
|
}
|