Files
continuwuity/src/main/server.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.3 KiB
Rust
Raw Normal View History

use std::{path::PathBuf, sync::Arc};
2024-05-09 15:59:08 -07:00
2025-09-27 12:05:38 +00:00
use tokio::sync::Mutex;
2025-04-22 01:41:02 +00:00
use tuwunel_core::{
Error, Result,
config::Config,
2025-09-27 12:05:38 +00:00
implement, info,
log::Log,
utils::{stream, sys},
};
2024-05-09 15:59:08 -07:00
use crate::{Args, args, logging::TracingFlameGuard, runtime};
2024-05-20 08:11:05 +00:00
2024-05-09 15:59:08 -07:00
/// Server runtime state; complete
2025-09-27 12:05:38 +00:00
pub struct Server {
2024-05-09 15:59:08 -07:00
/// Server runtime state; public portion
2025-09-27 12:05:38 +00:00
pub server: Arc<tuwunel_core::Server>,
2024-05-09 15:59:08 -07:00
2025-09-27 12:05:38 +00:00
pub services: Mutex<Option<Arc<tuwunel_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")]
_sentry_guard: Option<::sentry::ClientInitGuard>,
2024-05-09 15:59:08 -07:00
2025-04-22 01:41:02 +00:00
#[cfg(all(tuwunel_mods, feature = "tuwunel_mods"))]
2024-05-09 15:59:08 -07:00
// Module instances; TODO: move to mods::loaded mgmt vector
2025-04-22 01:41:02 +00:00
pub(crate) mods: tokio::sync::RwLock<Vec<tuwunel_core::mods::Module>>,
2024-05-09 15:59:08 -07:00
}
2025-09-27 12:05:38 +00:00
#[implement(Server)]
pub fn new(args: Option<&Args>, runtime: Option<&runtime::Handle>) -> Result<Arc<Self>, Error> {
let _runtime_guard = runtime.map(runtime::Handle::enter);
2025-09-27 12:05:38 +00:00
let args_default = args.is_none().then(Args::default);
let args = args.unwrap_or_else(|| args_default.as_ref().expect("default arguments"));
let config_paths = args
.config
.as_deref()
.into_iter()
.flat_map(<[_]>::iter)
.map(PathBuf::as_path);
2025-09-27 12:05:38 +00:00
let config = Config::load(config_paths)
.and_then(|raw| args::update(raw, args))
.and_then(|raw| Config::new(&raw))?;
2024-06-04 23:51:02 +00:00
2025-09-27 12:05:38 +00:00
let (tracing_reload_handle, tracing_flame_guard, capture) = crate::logging::init(&config)?;
2024-05-09 15:59:08 -07:00
2025-09-27 12:05:38 +00:00
config.check()?;
#[cfg(feature = "sentry_telemetry")]
let sentry_guard = crate::sentry::init(&config);
#[cfg(unix)]
sys::maximize_fd_limit()
.expect("Unable to increase maximum soft and hard file descriptor limit");
let (_old_width, _new_width) = stream::set_width(config.stream_width_default);
let (_old_amp, _new_amp) = stream::set_amplification(config.stream_amplification);
info!(
server_name = %config.server_name,
database_path = ?config.database_path,
log_levels = %config.log,
"{}",
tuwunel_core::version(),
);
let logger = Log { reload: tracing_reload_handle, capture };
Ok(Arc::new(Self {
server: Arc::new(tuwunel_core::Server::new(config, runtime.cloned(), logger)),
services: None.into(),
_tracing_flame_guard: tracing_flame_guard,
2025-01-28 19:42:09 +00:00
#[cfg(feature = "sentry_telemetry")]
2025-09-27 12:05:38 +00:00
_sentry_guard: sentry_guard,
2025-01-28 19:42:09 +00:00
2025-09-27 12:05:38 +00:00
#[cfg(all(tuwunel_mods, feature = "tuwunel_mods"))]
mods: tokio::sync::RwLock::new(Vec::new()),
}))
2024-05-09 15:59:08 -07:00
}