Files
VoelinClient/crates/voelin-gateway/src/main.rs
T
Claude 0028981714 tsgw: runtime settings, versioned database, pins, reactions, topics, events, streams, activity
Settings: every key is in one registry with type, default and
description. Values resolve as database (set at runtime) > --set >
TSGW_<KEY> environment > tsgw.toml > default; query login, addresses
and the database path are bootstrap-only. Changes are published on a
watch channel and applied live (relay nickname rebuilds the relay pool,
pinned channels open, retention re-prunes, capabilities are pushed).
SIGHUP or a changed file reloads it; values set at runtime keep
winning and differing keys are logged. ConfigReset falls back.

Database: user_version migrations keep existing data. Messages get
AUTOINCREMENT ids (never reused after pruning), topic ids and
revisions for sync; new tables for reactions, pins, topics, events,
RSVPs, reminder bookkeeping, the stream directory, activity and
settings. WAL with synchronous=NORMAL, cached statements, batched
inserts, an index for every query path (checked with EXPLAIN QUERY
PLAN on 12k messages). Retention 0 keeps forever; pinned messages
are kept.

No built-in caps: history pages are unlimited unless an admin sets
quota.history_page; pins, topics, events, reactions and streams have
quotas that default to none.

Permission rules per action (by server and channel groups) live in the
settings, with TeamSpeak-permission defaults. The stream directory
follows presence (left, stopped streaming, moved) and lists streams
without a registration as detected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-27 13:44:49 +00:00

93 lines
2.6 KiB
Rust

//! tsgw: companion gateway for TeamSpeak 3/6 servers.
//!
//! Gives app users invisible presence (who is in which channel) and channel
//! chat without joining voice, through ServerQuery, plus pins, reactions,
//! topics, events, a stream directory and an activity feed. See
//! docs/gateway-admin.md and the protocol in `voelin-gateway-proto`.
mod config;
mod db;
mod features;
mod hub;
mod perms;
mod session;
mod settings;
mod streams;
mod tasks;
#[cfg(test)]
mod tests;
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::Result;
use axum::Router;
use axum::extract::{State, WebSocketUpgrade};
use axum::response::IntoResponse;
use axum::routing::get;
use clap::Parser;
use tracing::{info, warn};
use tracing_subscriber::EnvFilter;
use crate::hub::Hub;
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
/// Configuration file.
#[arg(long, short, default_value = "tsgw.toml", env = "TSGW_CONFIG")]
config: PathBuf,
/// Set a key, e.g. `--set history.retention_days=90`; wins over the
/// environment and the file, but not over values set at runtime.
#[arg(long = "set", value_name = "KEY=VALUE")]
set: Vec<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
)
.init();
let args = Args::parse();
let layers = config::Layers::load(&args.config, &args.set)?;
let bind = layers.bootstrap()?.bind;
let hub = Hub::start(layers).await?;
tokio::spawn(reload_on_hangup(hub.clone()));
let listener = tokio::net::TcpListener::bind(bind).await?;
info!(%bind, "listening");
axum::serve(listener, router(hub)).with_graceful_shutdown(shutdown()).await?;
Ok(())
}
pub fn router(hub: Arc<Hub>) -> Router {
Router::new().route("/v1", get(ws)).route("/health", get(|| async { "ok" })).with_state(hub)
}
async fn ws(State(hub): State<Arc<Hub>>, upgrade: WebSocketUpgrade) -> impl IntoResponse {
upgrade
.protocols([voelin_gateway_proto::SUBPROTOCOL])
.on_upgrade(move |socket| session::run(hub, socket))
}
/// `SIGHUP` reads the configuration file again.
#[cfg(unix)]
async fn reload_on_hangup(hub: Arc<Hub>) {
use tokio::signal::unix::{SignalKind, signal};
let Ok(mut hangup) = signal(SignalKind::hangup()) else { return };
while hangup.recv().await.is_some() {
if let Err(error) = hub.settings.reload() {
warn!(error = format!("{error:#}"), "reload failed; keeping the current configuration");
}
}
}
#[cfg(not(unix))]
async fn reload_on_hangup(_hub: Arc<Hub>) {}
async fn shutdown() {
let _ = tokio::signal::ctrl_c().await;
info!("shutting down");
}