Add a API endpoint to create a user

This commit is contained in:
Valentin Tolmer 2021-05-30 18:26:14 +02:00
parent e3ff3b7b0a
commit ca48de542b
2 changed files with 18 additions and 3 deletions

View File

@ -28,6 +28,20 @@ where
.unwrap_or_else(error_to_api_response) .unwrap_or_else(error_to_api_response)
} }
async fn create_user_handler<Backend>(
data: web::Data<AppState<Backend>>,
info: web::Json<CreateUserRequest>,
) -> 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<Backend>(cfg: &mut web::ServiceConfig) pub fn api_config<Backend>(cfg: &mut web::ServiceConfig)
where where
Backend: TcpBackendHandler + BackendHandler + 'static, Backend: TcpBackendHandler + BackendHandler + 'static,
@ -44,10 +58,10 @@ where
) )
.into() .into()
}); });
cfg.app_data(json_config);
cfg.service(web::resource("/users").route(web::post().to(user_list_handler::<Backend>)));
cfg.service( cfg.service(
web::resource("/users") web::resource("/users/create").route(web::post().to(create_user_handler::<Backend>)),
.app_data(json_config)
.route(web::post().to(user_list_handler::<Backend>)),
); );
} }

View File

@ -57,6 +57,7 @@ fn http_config<Backend>(
auth_service::token_validator::<Backend>, auth_service::token_validator::<Backend>,
)) ))
.wrap(auth_service::CookieToHeaderTranslatorFactory) .wrap(auth_service::CookieToHeaderTranslatorFactory)
.guard(actix_web::guard::Header("content-type", "application/json"))
.configure(tcp_api::api_config::<Backend>), .configure(tcp_api::api_config::<Backend>),
) )
// Serve the /pkg path with the compiled WASM app. // Serve the /pkg path with the compiled WASM app.