From ca48de542b719b72ef42236d83fce608a370b8de Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 30 May 2021 18:26:14 +0200 Subject: [PATCH] Add a API endpoint to create a user --- src/infra/tcp_api.rs | 20 +++++++++++++++++--- src/infra/tcp_server.rs | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/infra/tcp_api.rs b/src/infra/tcp_api.rs index 8dd1644..f5d3775 100644 --- a/src/infra/tcp_api.rs +++ b/src/infra/tcp_api.rs @@ -28,6 +28,20 @@ where .unwrap_or_else(error_to_api_response) } +async fn create_user_handler( + data: web::Data>, + info: web::Json, +) -> ApiResult<()> +where + Backend: TcpBackendHandler + BackendHandler + 'static, +{ + data.backend_handler + .create_user(info.clone()) + .await + .map(|res| ApiResult::Left(web::Json(res))) + .unwrap_or_else(error_to_api_response) +} + pub fn api_config(cfg: &mut web::ServiceConfig) where Backend: TcpBackendHandler + BackendHandler + 'static, @@ -44,10 +58,10 @@ where ) .into() }); + cfg.app_data(json_config); + cfg.service(web::resource("/users").route(web::post().to(user_list_handler::))); cfg.service( - web::resource("/users") - .app_data(json_config) - .route(web::post().to(user_list_handler::)), + web::resource("/users/create").route(web::post().to(create_user_handler::)), ); } diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index 8190db3..1bee320 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -57,6 +57,7 @@ fn http_config( auth_service::token_validator::, )) .wrap(auth_service::CookieToHeaderTranslatorFactory) + .guard(actix_web::guard::Header("content-type", "application/json")) .configure(tcp_api::api_config::), ) // Serve the /pkg path with the compiled WASM app.