From 5a3332430f4debaf6b77ba215170690bb49cb382 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 30 Jun 2021 20:47:01 +0200 Subject: [PATCH] Add a route to server user details --- src/infra/tcp_api.rs | 30 +++++++++++++++++++++++++++--- src/infra/tcp_server.rs | 1 - 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/infra/tcp_api.rs b/src/infra/tcp_api.rs index d1050db..3e59612 100644 --- a/src/infra/tcp_api.rs +++ b/src/infra/tcp_api.rs @@ -5,7 +5,7 @@ use crate::{ tcp_server::{error_to_http_response, AppState}, }, }; -use actix_web::{web, HttpResponse}; +use actix_web::{web, HttpRequest, HttpResponse}; pub(crate) fn error_to_api_response(error: DomainError) -> ApiResult { ApiResult::Right(error_to_http_response(error)) @@ -28,6 +28,23 @@ where .unwrap_or_else(error_to_api_response) } +async fn user_details_handler( + data: web::Data>, + request: HttpRequest, +) -> ApiResult +where + Backend: TcpBackendHandler + BackendHandler + 'static, +{ + let request = UserDetailsRequest { + user_id: request.match_info().get("user_id").unwrap().to_string(), + }; + data.backend_handler + .get_user_details(request) + .await + .map(|res| ApiResult::Left(web::Json(res))) + .unwrap_or_else(error_to_api_response) +} + async fn create_user_handler( data: web::Data>, info: web::Json, @@ -59,9 +76,16 @@ where .into() }); cfg.app_data(json_config); - cfg.service(web::resource("/users").route(web::post().to(user_list_handler::))); cfg.service( - web::resource("/users/create").route(web::post().to(create_user_handler::)), + web::resource("/user/{user_id}").route(web::get().to(user_details_handler::)), + ); + cfg.service( + web::scope("/users") + .guard(actix_web::guard::Header("content-type", "application/json")) + .service(web::resource("").route(web::post().to(user_list_handler::))) + .service( + web::resource("/create").route(web::post().to(create_user_handler::)), + ), ); } diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index f3d16ff..a2d0697 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -68,7 +68,6 @@ 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.