forked from lavina/lavina
1
0
Fork 0
lavina/crates/lavina-core/src/lib.rs

75 lines
2.0 KiB
Rust
Raw Normal View History

2023-02-04 01:01:49 +00:00
//! Domain definitions and implementation of common chat logic.
use std::sync::Arc;
use anyhow::Result;
use prometheus::Registry as MetricsRegistry;
use crate::auth::Authenticator;
use crate::clustering::broadcast::Broadcasting;
use crate::clustering::{ClusterConfig, LavinaClient};
use crate::dialog::DialogRegistry;
use crate::player::PlayerRegistry;
use crate::repo::Storage;
use crate::room::RoomRegistry;
pub mod auth;
pub mod clustering;
pub mod dialog;
2023-02-04 01:01:49 +00:00
pub mod player;
pub mod prelude;
2023-07-07 13:09:24 +00:00
pub mod repo;
2023-02-04 01:01:49 +00:00
pub mod room;
pub mod terminator;
2023-09-30 23:12:11 +00:00
mod table;
#[derive(Clone)]
pub struct LavinaCore {
pub players: PlayerRegistry,
pub rooms: RoomRegistry,
pub dialogs: DialogRegistry,
pub broadcasting: Broadcasting,
pub authenticator: Authenticator,
}
impl LavinaCore {
pub async fn new(
mut metrics: MetricsRegistry,
cluster_config: ClusterConfig,
storage: Storage,
) -> Result<LavinaCore> {
// TODO shutdown all services in reverse order on error
let broadcasting = Broadcasting::new();
let client = LavinaClient::new(cluster_config.addresses.clone());
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
let dialogs = DialogRegistry::new(storage.clone());
let players = PlayerRegistry::empty(
rooms.clone(),
dialogs.clone(),
storage.clone(),
&mut metrics,
Arc::new(cluster_config.metadata),
client,
broadcasting.clone(),
)?;
dialogs.set_players(players.clone()).await;
let authenticator = Authenticator::new(storage.clone());
Ok(LavinaCore {
players,
rooms,
dialogs,
broadcasting,
authenticator,
})
}
pub async fn shutdown(mut self) -> Result<()> {
self.players.shutdown_all().await?;
self.dialogs.unset_players().await;
self.players.shutdown()?;
self.dialogs.shutdown()?;
self.rooms.shutdown()?;
Ok(())
}
}