From 4ae94839f24dcd73da10b291c8b0b380ac2a1d40 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 9 May 2021 11:52:53 +0200 Subject: [PATCH] Setup a basic API route --- Cargo.toml | 3 ++- app/src/app.rs | 1 + model/Cargo.toml | 2 +- model/src/lib.rs | 12 +++++++----- src/infra/tcp_server.rs | 32 +++++++++++++++++++++++++++++++- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee13d82..f60d02b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ actix-service = "2.0.0" actix-web = "4.0.0-beta.6" anyhow = "*" clap = "3.0.0-beta.2" -chrono = "*" +chrono = { version = "*", features = [ "serde" ]} futures = "*" futures-util = "*" http = "*" @@ -38,6 +38,7 @@ async-trait = "0.1.48" sea-query = { version = "0.9.4", features = [ "with-chrono" ] } lldap_model = { path = "model" } lldap_app = { path = "app" } +serde_json = "1.0.64" [dependencies.figment] features = ["toml", "env"] diff --git a/app/src/app.rs b/app/src/app.rs index 38b77d8..89770a2 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -8,6 +8,7 @@ pub enum Msg { ListUsersRequest(ListUsersRequest), } + impl Component for App { type Message = Msg; type Properties = (); diff --git a/model/Cargo.toml b/model/Cargo.toml index f4528de..b3b1c59 100644 --- a/model/Cargo.toml +++ b/model/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" js = [] [dependencies] -chrono = "*" +chrono = { version = "*", features = [ "serde" ]} serde = "*" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/model/src/lib.rs b/model/src/lib.rs index ba87c7b..2bac1a2 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -1,10 +1,12 @@ -#[derive(PartialEq, Eq, Debug)] +use serde::{Serialize, Deserialize}; + +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct BindRequest { pub name: String, pub password: String, } -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum RequestFilter { And(Vec), Or(Vec), @@ -12,12 +14,12 @@ pub enum RequestFilter { Equality(String, String), } -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct ListUsersRequest { pub filters: Option, } -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] #[cfg_attr(not(target_arch = "wasm32"), derive(sqlx::FromRow))] pub struct User { pub user_id: String, @@ -29,7 +31,7 @@ pub struct User { pub creation_date: chrono::NaiveDateTime, } -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct Group { pub display_name: String, pub users: Vec, diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index 7d5c022..dfec54c 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -4,7 +4,7 @@ use actix_files::{Files, NamedFile}; use actix_http::HttpServiceBuilder; use actix_server::ServerBuilder; use actix_service::map_config; -use actix_web::{dev::AppConfig, web, App, HttpRequest}; +use actix_web::{dev::AppConfig, web, App, HttpRequest, HttpResponse}; use anyhow::{Context, Result}; use std::path::PathBuf; @@ -16,6 +16,34 @@ async fn index(req: HttpRequest) -> actix_web::Result { Ok(NamedFile::open(path)?) } +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())) + } +} + +fn api_config(cfg: &mut web::ServiceConfig) { + let json_config = web::JsonConfig::default() + .limit(4096) + .error_handler(|err, _req| { + // create custom error response + actix_web::error::InternalError::from_response( + err, + HttpResponse::Conflict().finish().into(), + ) + .into() + }); + cfg.service( + web::resource("/users") + .app_data(json_config) + .route(web::post().to(user_list_handler)), + ); +} + pub fn build_tcp_server( config: &Configuration, _backend_handler: Backend, @@ -34,6 +62,8 @@ where "/{filename:(index\\.html|main\\.js)?}", web::get().to(index), ) + // API endpoint. + .service(web::scope("/api").configure(api_config)) // Serve the /pkg path with the compiled WASM app. .service(Files::new("/pkg", "./app/pkg")), |_| AppConfig::default(),