2024-05-09 15:59:08 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
use conduwuit::{config::Config, info, log::Log, utils::sys, Error, Result};
|
2024-07-27 07:17:07 +00:00
|
|
|
use tokio::{runtime, sync::Mutex};
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-12-08 13:47:02 +00:00
|
|
|
use crate::{clap::Args, logging::TracingFlameGuard};
|
2024-05-20 08:11:05 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
/// Server runtime state; complete
|
|
|
|
pub(crate) struct Server {
|
|
|
|
/// Server runtime state; public portion
|
2024-12-14 21:58:01 -05:00
|
|
|
pub(crate) server: Arc<conduwuit::Server>,
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
pub(crate) services: Mutex<Option<Arc<conduwuit_service::Services>>>,
|
2024-07-27 07:17:07 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
_tracing_flame_guard: TracingFlameGuard,
|
|
|
|
|
|
|
|
#[cfg(feature = "sentry_telemetry")]
|
2024-06-24 22:10:34 +00:00
|
|
|
_sentry_guard: Option<::sentry::ClientInitGuard>,
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
#[cfg(conduwuit_mods)]
|
2024-05-09 15:59:08 -07:00
|
|
|
// Module instances; TODO: move to mods::loaded mgmt vector
|
2024-12-14 21:58:01 -05:00
|
|
|
pub(crate) mods: tokio::sync::RwLock<Vec<conduwuit::mods::Module>>,
|
2024-05-09 15:59:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2024-12-17 14:32:54 +00:00
|
|
|
pub(crate) fn new(
|
2024-12-15 00:05:47 -05:00
|
|
|
args: &Args,
|
|
|
|
runtime: Option<&runtime::Handle>,
|
|
|
|
) -> Result<Arc<Self>, Error> {
|
2024-11-27 02:55:38 +00:00
|
|
|
let _runtime_guard = runtime.map(runtime::Handle::enter);
|
|
|
|
|
2024-10-04 19:10:20 +00:00
|
|
|
let raw_config = Config::load(args.config.as_deref())?;
|
2024-07-25 02:59:54 +00:00
|
|
|
let raw_config = crate::clap::update(raw_config, args)?;
|
|
|
|
let config = Config::new(&raw_config)?;
|
2024-06-04 23:51:02 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[cfg(feature = "sentry_telemetry")]
|
2024-06-24 22:10:34 +00:00
|
|
|
let sentry_guard = crate::sentry::init(&config);
|
|
|
|
|
2024-12-15 00:05:47 -05:00
|
|
|
let (tracing_reload_handle, tracing_flame_guard, capture) =
|
|
|
|
crate::logging::init(&config)?;
|
2024-05-09 15:59:08 -07:00
|
|
|
|
|
|
|
config.check()?;
|
2024-06-24 22:10:34 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
#[cfg(unix)]
|
2024-12-15 00:05:47 -05:00
|
|
|
sys::maximize_fd_limit()
|
|
|
|
.expect("Unable to increase maximum soft and hard file descriptor limit");
|
2024-06-04 23:51:02 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
info!(
|
|
|
|
server_name = %config.server_name,
|
|
|
|
database_path = ?config.database_path,
|
|
|
|
log_levels = %config.log,
|
|
|
|
"{}",
|
2024-12-14 21:58:01 -05:00
|
|
|
conduwuit::version(),
|
2024-05-09 15:59:08 -07:00
|
|
|
);
|
|
|
|
|
2024-06-09 10:23:06 +00:00
|
|
|
Ok(Arc::new(Self {
|
2024-12-15 00:05:47 -05:00
|
|
|
server: Arc::new(conduwuit::Server::new(config, runtime.cloned(), Log {
|
|
|
|
reload: tracing_reload_handle,
|
|
|
|
capture,
|
|
|
|
})),
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-27 07:17:07 +00:00
|
|
|
services: None.into(),
|
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
_tracing_flame_guard: tracing_flame_guard,
|
|
|
|
|
|
|
|
#[cfg(feature = "sentry_telemetry")]
|
|
|
|
_sentry_guard: sentry_guard,
|
|
|
|
|
2024-12-14 21:58:01 -05:00
|
|
|
#[cfg(conduwuit_mods)]
|
2024-05-09 15:59:08 -07:00
|
|
|
mods: tokio::sync::RwLock::new(Vec::new()),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|