pub mod authentication; pub mod backup; pub mod call; pub mod certificate; pub mod datagram; pub mod db; pub mod error; pub mod files; pub mod hub; pub mod network; pub mod publish; pub mod rate_limit; pub mod request; 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}; const SERVER_CONFIG_FILE_PATH: &str = concat!( env!("CARGO_MANIFEST_DIR"), "/../../configs/server_config.toml" ); fn config_path() -> PathBuf { std::env::var_os("YUMUSH_CONFIG_DIR") .map(|dir| PathBuf::from(dir).join("server_config.toml")) .unwrap_or_else(|| PathBuf::from(SERVER_CONFIG_FILE_PATH)) } pub struct ServerConfig { pub database_path: String, pub server_address: String, pub certificate_path: String, pub key_path: String, } 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")?, }) } 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 showtime() -> Result<(), ServerError> { 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 updates = sibling_directory(&config.database_path, "updates")?; tokio::spawn(files::collect(database.clone(), attachments.clone())); network::serve( &config.server_address, certificate, database, attachments, backups, updates, ) .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)?; db::user::set_role_by_id(user.id, role, db).await?; Ok(()) }) .await }