Setup a basic API route

This commit is contained in:
Valentin Tolmer 2021-05-09 11:52:53 +02:00
parent 4091d21277
commit 4ae94839f2
5 changed files with 42 additions and 8 deletions

View File

@ -19,7 +19,7 @@ actix-service = "2.0.0"
actix-web = "4.0.0-beta.6" actix-web = "4.0.0-beta.6"
anyhow = "*" anyhow = "*"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
chrono = "*" chrono = { version = "*", features = [ "serde" ]}
futures = "*" futures = "*"
futures-util = "*" futures-util = "*"
http = "*" http = "*"
@ -38,6 +38,7 @@ async-trait = "0.1.48"
sea-query = { version = "0.9.4", features = [ "with-chrono" ] } sea-query = { version = "0.9.4", features = [ "with-chrono" ] }
lldap_model = { path = "model" } lldap_model = { path = "model" }
lldap_app = { path = "app" } lldap_app = { path = "app" }
serde_json = "1.0.64"
[dependencies.figment] [dependencies.figment]
features = ["toml", "env"] features = ["toml", "env"]

View File

@ -8,6 +8,7 @@ pub enum Msg {
ListUsersRequest(ListUsersRequest), ListUsersRequest(ListUsersRequest),
} }
impl Component for App { impl Component for App {
type Message = Msg; type Message = Msg;
type Properties = (); type Properties = ();

View File

@ -8,7 +8,7 @@ edition = "2018"
js = [] js = []
[dependencies] [dependencies]
chrono = "*" chrono = { version = "*", features = [ "serde" ]}
serde = "*" serde = "*"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -1,10 +1,12 @@
#[derive(PartialEq, Eq, Debug)] use serde::{Serialize, Deserialize};
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct BindRequest { pub struct BindRequest {
pub name: String, pub name: String,
pub password: String, pub password: String,
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub enum RequestFilter { pub enum RequestFilter {
And(Vec<RequestFilter>), And(Vec<RequestFilter>),
Or(Vec<RequestFilter>), Or(Vec<RequestFilter>),
@ -12,12 +14,12 @@ pub enum RequestFilter {
Equality(String, String), Equality(String, String),
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct ListUsersRequest { pub struct ListUsersRequest {
pub filters: Option<RequestFilter>, pub filters: Option<RequestFilter>,
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
#[cfg_attr(not(target_arch = "wasm32"), derive(sqlx::FromRow))] #[cfg_attr(not(target_arch = "wasm32"), derive(sqlx::FromRow))]
pub struct User { pub struct User {
pub user_id: String, pub user_id: String,
@ -29,7 +31,7 @@ pub struct User {
pub creation_date: chrono::NaiveDateTime, pub creation_date: chrono::NaiveDateTime,
} }
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Group { pub struct Group {
pub display_name: String, pub display_name: String,
pub users: Vec<String>, pub users: Vec<String>,

View File

@ -4,7 +4,7 @@ use actix_files::{Files, NamedFile};
use actix_http::HttpServiceBuilder; use actix_http::HttpServiceBuilder;
use actix_server::ServerBuilder; use actix_server::ServerBuilder;
use actix_service::map_config; 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 anyhow::{Context, Result};
use std::path::PathBuf; use std::path::PathBuf;
@ -16,6 +16,34 @@ async fn index(req: HttpRequest) -> actix_web::Result<NamedFile> {
Ok(NamedFile::open(path)?) Ok(NamedFile::open(path)?)
} }
type ApiResult<M> = actix_web::Either<web::Json<M>, HttpResponse>;
async fn user_list_handler(_info: web::Json<ListUsersRequest>) -> ApiResult<Vec<User>> {
if true {
ApiResult::Right(HttpResponse::Conflict().finish())
} else {
ApiResult::Left(web::Json(Vec::<User>::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<Backend>( pub fn build_tcp_server<Backend>(
config: &Configuration, config: &Configuration,
_backend_handler: Backend, _backend_handler: Backend,
@ -34,6 +62,8 @@ where
"/{filename:(index\\.html|main\\.js)?}", "/{filename:(index\\.html|main\\.js)?}",
web::get().to(index), web::get().to(index),
) )
// API endpoint.
.service(web::scope("/api").configure(api_config))
// Serve the /pkg path with the compiled WASM app. // Serve the /pkg path with the compiled WASM app.
.service(Files::new("/pkg", "./app/pkg")), .service(Files::new("/pkg", "./app/pkg")),
|_| AppConfig::default(), |_| AppConfig::default(),