forked from lavina/lavina
65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
//! Domain definitions and implementation of common chat logic.
|
|
use crate::clustering::{ClusterConfig, LavinaClient};
|
|
use anyhow::Result;
|
|
use prometheus::Registry as MetricsRegistry;
|
|
use std::sync::Arc;
|
|
|
|
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;
|
|
pub mod player;
|
|
pub mod prelude;
|
|
pub mod repo;
|
|
pub mod room;
|
|
pub mod terminator;
|
|
|
|
mod table;
|
|
|
|
#[derive(Clone)]
|
|
pub struct LavinaCore {
|
|
pub players: PlayerRegistry,
|
|
pub rooms: RoomRegistry,
|
|
pub dialogs: DialogRegistry,
|
|
}
|
|
|
|
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 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,
|
|
)?;
|
|
dialogs.set_players(players.clone()).await;
|
|
Ok(LavinaCore {
|
|
players,
|
|
rooms,
|
|
dialogs,
|
|
})
|
|
}
|
|
|
|
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(())
|
|
}
|
|
}
|