nullnet_liblogging/datastore/
config.rs

1use crate::datastore::grpc_interface::GrpcInterface;
2use nullnet_libappguard::AppGuardGrpcInterface;
3use nullnet_libwallguard::WallGuardGrpcInterface;
4use std::sync::Arc;
5use std::time::Duration;
6use tokio::sync::RwLock;
7use tokio::time::sleep;
8
9pub struct DatastoreConfig {
10    pub(crate) token: Arc<RwLock<String>>,
11    pub(crate) server_kind: ServerKind,
12    pub(crate) addr: String,
13    pub(crate) port: u16,
14    pub(crate) tls: bool,
15}
16
17impl DatastoreConfig {
18    /// Creates a new `DatastoreConfig` instance.
19    ///
20    /// # Arguments
21    ///
22    /// * `token` - The token to use for authenticating to datastore.
23    /// * `server_kind` - The kind of server to connect to (i.e., `AppGuard` or `WallGuard`).
24    /// * `addr` - The IP address of the server (use 0.0.0.0 if running from the server itself).
25    /// * `port` - The port of the server.
26    /// * `tls` - Whether to use TLS or not for communication with the server.
27    #[allow(clippy::missing_errors_doc)]
28    #[must_use]
29    pub fn new(
30        token: Arc<RwLock<String>>,
31        server_kind: ServerKind,
32        addr: String,
33        port: u16,
34        tls: bool,
35    ) -> Self {
36        Self {
37            token,
38            server_kind,
39            addr,
40            port,
41            tls,
42        }
43    }
44
45    pub(crate) async fn connect(&self) -> GrpcInterface {
46        // Create a new gRPC client based on the server kind
47        loop {
48            match self.server_kind {
49                ServerKind::AppGuard => {
50                    match AppGuardGrpcInterface::new(&self.addr, self.port, self.tls).await {
51                        Ok(client) => return GrpcInterface::AppGuard(client),
52                        Err(_) => {
53                            sleep(Duration::from_secs(1)).await;
54                        }
55                    }
56                }
57                ServerKind::WallGuard => {
58                    return GrpcInterface::WallGuard(
59                        WallGuardGrpcInterface::new(&self.addr, self.port).await,
60                    );
61                }
62            }
63        }
64    }
65}
66
67pub enum ServerKind {
68    AppGuard,
69    WallGuard,
70}