Files
rhema-together/src/config.rs
T
2026-11-29 21:34:09 +01:00

128 lines
4.7 KiB
Rust

//! Process configuration: one YAML file, environment overrides.
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
/// The server's settings. Every field has a default that works from the
/// repository root, so `cargo run` starts without a config file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// Address the HTTP server binds.
pub listen: String,
/// The URL browsers use. Cookies and invite links are built from it.
pub public_url: String,
/// The origin previews are served from. Empty disables the preview
/// proxy.
#[serde(default)]
pub preview_origin: String,
/// SQLite file.
pub database: PathBuf,
/// The 32-byte symmetric key protecting stored provider keys and ssh
/// keys, base64 or raw. Created on first start when missing.
pub secret_key_file: PathBuf,
/// The built Nuxt client.
pub web_dir: PathBuf,
/// Cap on agent steps in one turn.
#[serde(default = "default_steps")]
pub max_turn_steps: u32,
/// Extra origins allowed to frame the app. Empty is none.
#[serde(default)]
pub allowed_frame_ancestors: Vec<String>,
/// Whether anyone may create an account. Off leaves setup and
/// invites as the only doors.
#[serde(default)]
pub allow_registration: bool,
/// Whether a sandbox target may point at a loopback, private,
/// link-local, or multicast address. Off by default, so a target
/// cannot turn the server into a probe of its own network.
#[serde(default)]
pub allow_private_targets: bool,
}
fn default_steps() -> u32 {
48
}
impl Default for Config {
fn default() -> Self {
Self {
listen: "127.0.0.1:7800".into(),
public_url: "http://localhost:7800".into(),
preview_origin: "http://preview.localhost:7800".into(),
database: PathBuf::from("data/together.db"),
secret_key_file: PathBuf::from("data/secret.key"),
web_dir: PathBuf::from("web/.output/public"),
max_turn_steps: default_steps(),
allowed_frame_ancestors: Vec::new(),
allow_registration: false,
allow_private_targets: false,
}
}
}
impl Config {
/// Reads `path` when it exists, then applies the `TOGETHER_*`
/// environment variables. A missing file is the defaults.
pub fn load(path: &std::path::Path) -> anyhow::Result<Self> {
let mut config = if path.exists() {
let text = std::fs::read_to_string(path)?;
serde_yaml::from_str(&text)?
} else {
Self::default()
};
if let Ok(value) = std::env::var("TOGETHER_LISTEN") {
config.listen = value;
}
if let Ok(value) = std::env::var("TOGETHER_PUBLIC_URL") {
config.public_url = value;
}
if let Ok(value) = std::env::var("TOGETHER_PREVIEW_ORIGIN") {
config.preview_origin = value;
}
if let Ok(value) = std::env::var("TOGETHER_DATABASE") {
config.database = value.into();
}
if let Ok(value) = std::env::var("TOGETHER_SECRET_KEY_FILE") {
config.secret_key_file = value.into();
}
if let Ok(value) = std::env::var("TOGETHER_WEB_DIR") {
config.web_dir = value.into();
}
if let Ok(value) = std::env::var("TOGETHER_MAX_TURN_STEPS") {
config.max_turn_steps = value.parse()?;
}
if let Ok(value) = std::env::var("TOGETHER_ALLOW_REGISTRATION") {
config.allow_registration = matches!(value.as_str(), "1" | "true" | "yes");
}
if let Ok(value) = std::env::var("TOGETHER_ALLOW_PRIVATE_TARGETS") {
config.allow_private_targets = matches!(value.as_str(), "1" | "true" | "yes");
}
config.public_url = config.public_url.trim_end_matches('/').to_owned();
config.preview_origin = config.preview_origin.trim_end_matches('/').to_owned();
Ok(config)
}
/// Whether cookies should carry `Secure`.
pub fn secure_cookies(&self) -> bool {
self.public_url.starts_with("https://")
}
/// `scheme://*.<preview host>`, for the CSP that lets the app frame
/// any room's preview. `None` when previews are off.
pub fn preview_frame_source(&self) -> Option<String> {
let (scheme, rest) = self.preview_origin.split_once("://")?;
let host = rest.split('/').next()?;
let (name, port) = match host.rsplit_once(':') {
Some((name, port)) if port.chars().all(|c| c.is_ascii_digit()) => {
(name, format!(":{port}"))
}
_ => (host, String::new()),
};
if name.is_empty() {
return None;
}
Some(format!("{scheme}://*.{name}{port}"))
}
}