pub mod authentication; pub mod backup; pub mod call; pub mod certificate; pub mod db; pub mod error; pub mod files; pub mod forge; pub mod hub; pub mod network; pub mod publish; pub mod rate_limit; pub mod retention; pub mod rooms; pub mod stats; pub mod update; #[cfg(test)] mod test; use std::path::{Path, PathBuf}; use common::{config::Config, error::Error as CommonError, model::Role}; use crate::{db::Db, error::ServerError}; fn config_path() -> PathBuf { std::env::var_os("YUMUSH_CONFIG_DIR") .map(PathBuf::from) .unwrap_or_else(|| PathBuf::from("configs")) .join("server_config.toml") } struct ServerConfig { database_path: String, server_address: String, certificate_path: String, key_path: String, forge: Option, forge_token: Option, } fn load_config() -> Result { let content = std::fs::read_to_string(config_path())?; let config = Config::parse(&content)?; Ok(ServerConfig { database_path: config.get("database_path")?, server_address: config.get("server_address")?, certificate_path: config.get("certificate_path")?, key_path: config.get("key_path")?, forge: config.get("forge").ok(), forge_token: config.get("forge_token").ok(), }) } fn sibling_directory(database_path: &str, name: &str) -> Result { let directory = Path::new(database_path) .parent() .unwrap_or_else(|| Path::new("")) .join(name); std::fs::create_dir_all(&directory)?; Ok(directory) } pub async fn run() -> Result { let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); let config = load_config()?; let database = Db::open(&config.database_path).await?; let certificate = certificate::load(&config.certificate_path, &config.key_path)?; let attachments = sibling_directory(&config.database_path, "attachments")?; let backups = sibling_directory(&config.database_path, "backups")?; let releases = update::Releases::load( sibling_directory(&config.database_path, "clients")?, config .forge .as_deref() .and_then(|repo| forge::Forge::new(repo, config.forge_token.clone())), &database, ) .await?; network::serve( &config.server_address, certificate, database, attachments, backups, releases, ) .await } pub async fn set_role(username: &str, role: Role) -> Result<(), ServerError> { let config = load_config()?; let database = Db::open(&config.database_path).await?; database .run(async |db| { let user = db::user::by_username(username, db) .await? .ok_or(CommonError::NotFound)?; match role { Role::Builder => db::user::make_builder(user.id, db).await?, role => db::user::set_role(user.id, role, db).await?, }; Ok(()) }) .await }