diff --git a/model/src/lib.rs b/model/src/lib.rs index 2bac1a2..df7aa16 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -1,12 +1,12 @@ use serde::{Serialize, Deserialize}; -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub struct BindRequest { pub name: String, pub password: String, } -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub enum RequestFilter { And(Vec), Or(Vec), @@ -14,7 +14,7 @@ pub enum RequestFilter { Equality(String, String), } -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub struct ListUsersRequest { pub filters: Option, } diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index dfec54c..606b6bf 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -18,15 +18,24 @@ async fn index(req: HttpRequest) -> actix_web::Result { type ApiResult = actix_web::Either, HttpResponse>; -async fn user_list_handler(_info: web::Json) -> ApiResult> { - if true { - ApiResult::Right(HttpResponse::Conflict().finish()) - } else { - ApiResult::Left(web::Json(Vec::::new())) +async fn user_list_handler( + data: web::Data>, + info: web::Json, +) -> ApiResult> +where + Backend: BackendHandler + 'static, +{ + let req : ListUsersRequest = info.clone(); + match data.backend_handler.list_users(req).await { + Ok(res) => ApiResult::Left(web::Json(res)), + Err(_) => ApiResult::Right(HttpResponse::InternalServerError().finish()) } } -fn api_config(cfg: &mut web::ServiceConfig) { +fn api_config(cfg: &mut web::ServiceConfig) +where + Backend: BackendHandler + 'static, +{ let json_config = web::JsonConfig::default() .limit(4096) .error_handler(|err, _req| { @@ -40,13 +49,20 @@ fn api_config(cfg: &mut web::ServiceConfig) { cfg.service( web::resource("/users") .app_data(json_config) - .route(web::post().to(user_list_handler)), + .route(web::post().to(user_list_handler::)), ); } +struct AppState +where + Backend: BackendHandler + 'static, +{ + pub backend_handler: Backend, +} + pub fn build_tcp_server( config: &Configuration, - _backend_handler: Backend, + backend_handler: Backend, server_builder: ServerBuilder, ) -> Result where @@ -57,13 +73,16 @@ where HttpServiceBuilder::new() .finish(map_config( App::new() + .data(AppState:: { + backend_handler: backend_handler.clone(), + }) // Serve index.html and main.js, and default to index.html. .route( "/{filename:(index\\.html|main\\.js)?}", web::get().to(index), ) // API endpoint. - .service(web::scope("/api").configure(api_config)) + .service(web::scope("/api").configure(api_config::)) // Serve the /pkg path with the compiled WASM app. .service(Files::new("/pkg", "./app/pkg")), |_| AppConfig::default(),