Introduce BackendHandler trait and impl

This commit is contained in:
Valentin Tolmer
2021-03-11 10:14:15 +01:00
parent 1a947358fa
commit ff4e986a0d
7 changed files with 98 additions and 30 deletions

49
src/domain/handler.rs Normal file
View File

@@ -0,0 +1,49 @@
use crate::infra::configuration::Configuration;
use anyhow::{bail, Result};
use sqlx::any::AnyPool;
pub struct BindRequest {
pub name: String,
pub password: String,
}
pub struct SearchRequest {}
pub struct SearchResponse {}
pub trait BackendHandler: Clone + Send {
fn bind(&mut self, request: BindRequest) -> Result<()>;
fn search(&mut self, request: SearchRequest) -> Result<SearchResponse>;
}
#[derive(Debug, Clone)]
pub struct SqlBackendHandler {
config: Configuration,
sql_pool: AnyPool,
authenticated: bool,
}
impl SqlBackendHandler {
pub fn new(config: Configuration, sql_pool: AnyPool) -> Self {
SqlBackendHandler {
config,
sql_pool,
authenticated: false,
}
}
}
impl BackendHandler for SqlBackendHandler {
fn bind(&mut self, request: BindRequest) -> Result<()> {
if request.name == self.config.admin_dn && request.password == self.config.admin_password {
self.authenticated = true;
Ok(())
} else {
bail!(r#"Authentication error for "{}""#, request.name)
}
}
fn search(&mut self, request: SearchRequest) -> Result<SearchResponse> {
Ok(SearchResponse {})
}
}