use crate::datastore::grpc_interface::GrpcInterface;
use nullnet_libappguard::AppGuardGrpcInterface;
use nullnet_libwallguard::WallGuardGrpcInterface;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::time::sleep;
pub struct DatastoreConfig {
pub(crate) token: Arc<RwLock<String>>,
pub(crate) server_kind: ServerKind,
pub(crate) addr: String,
pub(crate) port: u16,
pub(crate) tls: bool,
}
impl DatastoreConfig {
#[allow(clippy::missing_errors_doc)]
#[must_use]
pub fn new(
token: Arc<RwLock<String>>,
server_kind: ServerKind,
addr: String,
port: u16,
tls: bool,
) -> Self {
Self {
token,
server_kind,
addr,
port,
tls,
}
}
pub(crate) async fn connect(&self) -> GrpcInterface {
loop {
match self.server_kind {
ServerKind::AppGuard => {
match AppGuardGrpcInterface::new(&self.addr, self.port, self.tls).await {
Ok(client) => return GrpcInterface::AppGuard(client),
Err(_) => {
sleep(Duration::from_secs(1)).await;
}
}
}
ServerKind::WallGuard => {
return GrpcInterface::WallGuard(
WallGuardGrpcInterface::new(&self.addr, self.port).await,
);
}
}
}
}
}
pub enum ServerKind {
AppGuard,
WallGuard,
}