pub mod errors;
use broker::broker_client::BrokerClient;
use db::db_handler_client::DbHandlerClient;
use log::info;
use service_registry::{service_registry_client::ServiceRegistryClient, GetServiceRequest};
use tonic::transport::Channel;
use ulid::{DecodeError, Ulid};
#[path = "codegen/db.rs"]
pub mod db;
#[path = "codegen/service_registry.rs"]
pub mod service_registry;
#[path = "codegen/broker.rs"]
pub mod broker;
pub async fn get_datastore_client(
service_registry_channel: Channel,
) -> Result<DbHandlerClient<Channel>, tonic::Status> {
let mut service_registry_client: ServiceRegistryClient<Channel> =
ServiceRegistryClient::new(service_registry_channel);
let datastore_url = match service_registry_client
.get_service(GetServiceRequest {
service_name: String::from("db"),
})
.await
{
Ok(response) => {
let resp = response.into_inner();
format!("http://{}:{}", resp.service_address, resp.service_port)
}
Err(e) => match e.code() {
tonic::Code::NotFound => {
return Err(tonic::Status::new(
tonic::Code::NotFound,
"Message broker not found in service registry",
))
}
_ => return Err(tonic::Status::new(tonic::Code::Internal, e.to_string())),
},
};
DbHandlerClient::connect(datastore_url)
.await
.map_err(|e| tonic::Status::new(tonic::Code::Unavailable, e.to_string()))
}
pub async fn get_message_broker_client(
message_broker_channel: Channel,
) -> Result<BrokerClient<Channel>, tonic::Status> {
let mut service_registry_client: ServiceRegistryClient<Channel> =
ServiceRegistryClient::new(message_broker_channel);
let message_broker_url = match service_registry_client
.get_service(GetServiceRequest {
service_name: String::from("message_broker"),
})
.await
{
Ok(response) => {
let resp = response.into_inner();
format!("http://{}:{}", resp.service_address, resp.service_port)
}
Err(e) => match e.code() {
tonic::Code::NotFound => {
return Err(tonic::Status::new(
tonic::Code::NotFound,
"Message broker not found in service registry",
))
}
_ => return Err(tonic::Status::new(tonic::Code::Internal, e.to_string())),
},
};
BrokerClient::connect(message_broker_url)
.await
.map_err(|e| tonic::Status::new(tonic::Code::Unavailable, e.to_string()))
}
pub fn parse_ulid(id: &str) -> Result<Ulid, DecodeError> {
let res = id.parse::<Ulid>();
match res {
Ok(ulid) => Ok(ulid),
Err(e) => {
info!("Error parsing ULID: {}", e);
Err(e)
}
}
}