use crate::admin::{AdminServer, AdminServerBuildError};
use crate::core::{HomeserverBuildError, HomeserverCore};
use crate::tracing::init_tracing_logs_with_config_if_set;
#[cfg(any(test, feature = "testing"))]
use crate::MockDataDir;
use crate::{app_context::AppContext, data_directory::PersistentDataDir};
use anyhow::Result;
use pkarr::PublicKey;
use std::path::PathBuf;
#[derive(thiserror::Error, Debug)]
pub enum HomeserverSuiteBuildError {
#[error("Failed to build homeserver: {0}")]
Homeserver(HomeserverBuildError),
#[error("Failed to build admin server: {0}")]
Admin(AdminServerBuildError),
}
pub struct HomeserverSuite {
context: AppContext,
#[allow(dead_code)] core: HomeserverCore,
#[allow(dead_code)] admin_server: AdminServer,
}
impl HomeserverSuite {
pub async fn start_with_persistent_data_dir_path(dir_path: PathBuf) -> Result<Self> {
let data_dir = PersistentDataDir::new(dir_path);
let context = AppContext::try_from(data_dir)?;
Self::start(context).await
}
pub async fn start_with_persistent_data_dir(dir: PersistentDataDir) -> Result<Self> {
let context = AppContext::try_from(dir)?;
Self::start(context).await
}
#[cfg(any(test, feature = "testing"))]
pub async fn start_with_mock_data_dir(dir: MockDataDir) -> Result<Self> {
let context = AppContext::try_from(dir)?;
Self::start(context).await
}
pub async fn start(context: AppContext) -> Result<Self> {
let _ = init_tracing_logs_with_config_if_set(&context.config_toml);
tracing::debug!("Homeserver data dir: {}", context.data_dir.path().display());
let core = HomeserverCore::new(context.clone()).await?;
let admin_server = AdminServer::start(&context).await?;
Ok(Self {
context,
core,
admin_server,
})
}
pub fn core(&self) -> &HomeserverCore {
&self.core
}
pub fn admin(&self) -> &AdminServer {
&self.admin_server
}
pub fn public_key(&self) -> PublicKey {
self.context.keypair.public_key()
}
pub fn pubky_url(&self) -> url::Url {
url::Url::parse(&format!("https://{}", self.public_key())).expect("valid url")
}
pub fn icann_http_url(&self) -> url::Url {
url::Url::parse(&self.core.icann_http_url()).expect("valid url")
}
}