mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
Add a handler for the /api/users endpoint
This commit is contained in:
parent
4ae94839f2
commit
8dd55195f5
@ -1,12 +1,12 @@
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct BindRequest {
|
pub struct BindRequest {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
|
||||||
pub enum RequestFilter {
|
pub enum RequestFilter {
|
||||||
And(Vec<RequestFilter>),
|
And(Vec<RequestFilter>),
|
||||||
Or(Vec<RequestFilter>),
|
Or(Vec<RequestFilter>),
|
||||||
@ -14,7 +14,7 @@ pub enum RequestFilter {
|
|||||||
Equality(String, String),
|
Equality(String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct ListUsersRequest {
|
pub struct ListUsersRequest {
|
||||||
pub filters: Option<RequestFilter>,
|
pub filters: Option<RequestFilter>,
|
||||||
}
|
}
|
||||||
|
@ -18,15 +18,24 @@ async fn index(req: HttpRequest) -> actix_web::Result<NamedFile> {
|
|||||||
|
|
||||||
type ApiResult<M> = actix_web::Either<web::Json<M>, HttpResponse>;
|
type ApiResult<M> = actix_web::Either<web::Json<M>, HttpResponse>;
|
||||||
|
|
||||||
async fn user_list_handler(_info: web::Json<ListUsersRequest>) -> ApiResult<Vec<User>> {
|
async fn user_list_handler<Backend>(
|
||||||
if true {
|
data: web::Data<AppState<Backend>>,
|
||||||
ApiResult::Right(HttpResponse::Conflict().finish())
|
info: web::Json<ListUsersRequest>,
|
||||||
} else {
|
) -> ApiResult<Vec<User>>
|
||||||
ApiResult::Left(web::Json(Vec::<User>::new()))
|
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<Backend>(cfg: &mut web::ServiceConfig)
|
||||||
|
where
|
||||||
|
Backend: BackendHandler + 'static,
|
||||||
|
{
|
||||||
let json_config = web::JsonConfig::default()
|
let json_config = web::JsonConfig::default()
|
||||||
.limit(4096)
|
.limit(4096)
|
||||||
.error_handler(|err, _req| {
|
.error_handler(|err, _req| {
|
||||||
@ -40,13 +49,20 @@ fn api_config(cfg: &mut web::ServiceConfig) {
|
|||||||
cfg.service(
|
cfg.service(
|
||||||
web::resource("/users")
|
web::resource("/users")
|
||||||
.app_data(json_config)
|
.app_data(json_config)
|
||||||
.route(web::post().to(user_list_handler)),
|
.route(web::post().to(user_list_handler::<Backend>)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct AppState<Backend>
|
||||||
|
where
|
||||||
|
Backend: BackendHandler + 'static,
|
||||||
|
{
|
||||||
|
pub backend_handler: Backend,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn build_tcp_server<Backend>(
|
pub fn build_tcp_server<Backend>(
|
||||||
config: &Configuration,
|
config: &Configuration,
|
||||||
_backend_handler: Backend,
|
backend_handler: Backend,
|
||||||
server_builder: ServerBuilder,
|
server_builder: ServerBuilder,
|
||||||
) -> Result<ServerBuilder>
|
) -> Result<ServerBuilder>
|
||||||
where
|
where
|
||||||
@ -57,13 +73,16 @@ where
|
|||||||
HttpServiceBuilder::new()
|
HttpServiceBuilder::new()
|
||||||
.finish(map_config(
|
.finish(map_config(
|
||||||
App::new()
|
App::new()
|
||||||
|
.data(AppState::<Backend> {
|
||||||
|
backend_handler: backend_handler.clone(),
|
||||||
|
})
|
||||||
// Serve index.html and main.js, and default to index.html.
|
// Serve index.html and main.js, and default to index.html.
|
||||||
.route(
|
.route(
|
||||||
"/{filename:(index\\.html|main\\.js)?}",
|
"/{filename:(index\\.html|main\\.js)?}",
|
||||||
web::get().to(index),
|
web::get().to(index),
|
||||||
)
|
)
|
||||||
// API endpoint.
|
// API endpoint.
|
||||||
.service(web::scope("/api").configure(api_config))
|
.service(web::scope("/api").configure(api_config::<Backend>))
|
||||||
// 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(),
|
||||||
|
Loading…
Reference in New Issue
Block a user