use svc_gis_client_grpc::prelude::Client;
use svc_gis_client_grpc::prelude::GisClient;
use svc_storage_client_grpc::prelude::Clients;
use tokio::sync::OnceCell;
pub(crate) static CLIENTS: OnceCell<GrpcClients> = OnceCell::const_new();
pub async fn get_clients() -> &'static GrpcClients {
CLIENTS
.get_or_init(|| async move {
let config = crate::Config::try_from_env().unwrap_or_default();
GrpcClients::default(config)
})
.await
}
#[derive(Clone, Debug)]
pub struct GrpcClients {
pub storage: Clients,
pub gis: GisClient,
}
impl GrpcClients {
pub fn default(config: crate::config::Config) -> Self {
let storage_clients = Clients::new(config.storage_host_grpc, config.storage_port_grpc);
GrpcClients {
storage: storage_clients,
gis: GisClient::new_client(&config.gis_host_grpc, config.gis_port_grpc, "gis"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use lib_common::grpc::Client;
use svc_gis_client_grpc::prelude::Client as GisClient;
#[tokio::test]
async fn test_grpc_clients_default() {
crate::get_log_handle().await;
ut_info!("(test_grpc_clients_default) Start.");
let clients = get_clients().await;
let vehicle = &clients.storage.vehicle;
ut_debug!("(test_grpc_clients_default) vehicle: {:?}", vehicle);
assert_eq!(vehicle.get_name(), "vehicle");
let vertipad = &clients.storage.vertipad;
ut_debug!("(test_grpc_clients_default) vertipad: {:?}", vertipad);
assert_eq!(vertipad.get_name(), "vertipad");
let vertiport = &clients.storage.vertiport;
ut_debug!("(test_grpc_clients_default) vertiport: {:?}", vertiport);
assert_eq!(vertiport.get_name(), "vertiport");
let itinerary = &clients.storage.itinerary;
ut_debug!("(test_grpc_clients_default) itinerary: {:?}", itinerary);
assert_eq!(itinerary.get_name(), "itinerary");
let itinerary_flight_plan = &clients.storage.itinerary_flight_plan_link;
ut_debug!(
"(test_grpc_clients_default) itinerary_flight_plan: {:?}",
itinerary_flight_plan
);
assert_eq!(
itinerary_flight_plan.get_name(),
"itinerary_flight_plan_link"
);
let flight_plan = &clients.storage.flight_plan;
ut_debug!("(test_grpc_clients_default) flight_plan: {:?}", flight_plan);
assert_eq!(flight_plan.get_name(), "flight_plan");
let gis = &clients.gis;
ut_debug!("(test_grpc_clients_default) gis: {:?}", gis);
assert_eq!(gis.get_name(), "gis");
ut_info!("(test_grpc_clients_default) Success.");
}
}