| 1 | pub mod authentication; |
| 2 | pub mod backup; |
| 3 | pub mod call; |
| 4 | pub mod certificate; |
| 5 | pub mod datagram; |
| 6 | pub mod db; |
| 7 | pub mod error; |
| 8 | pub mod files; |
| 9 | pub mod hub; |
| 10 | pub mod network; |
| 11 | pub mod publish; |
| 12 | pub mod rate_limit; |
| 13 | pub mod request; |
| 14 | pub mod retention; |
| 15 | pub mod rooms; |
| 16 | pub mod stats; |
| 17 | pub mod update; |
| 18 | |
| 19 | #[cfg(test)] |
| 20 | mod test; |
| 21 | |
| 22 | use std::path::{Path, PathBuf}; |
| 23 | |
| 24 | use common::{config::Config, error::Error as CommonError, model::Role}; |
| 25 | |
| 26 | use crate::{db::Db, error::ServerError}; |
| 27 | |
| 28 | const SERVER_CONFIG_FILE_PATH: &str = concat!( |
| 29 | env!("CARGO_MANIFEST_DIR"), |
| 30 | "/../../configs/server_config.toml" |
| 31 | ); |
| 32 | |
| 33 | fn config_path() -> PathBuf { |
| 34 | std::env::var_os("YUMUSH_CONFIG_DIR") |
| 35 | .map(|dir| PathBuf::from(dir).join("server_config.toml")) |
| 36 | .unwrap_or_else(|| PathBuf::from(SERVER_CONFIG_FILE_PATH)) |
| 37 | } |
| 38 | |
| 39 | pub struct ServerConfig { |
| 40 | pub database_path: String, |
| 41 | pub server_address: String, |
| 42 | pub certificate_path: String, |
| 43 | pub key_path: String, |
| 44 | } |
| 45 | |
| 46 | fn load_config() -> Result<ServerConfig, ServerError> { |
| 47 | let content = std::fs::read_to_string(config_path())?; |
| 48 | let config = Config::parse(&content)?; |
| 49 | |
| 50 | Ok(ServerConfig { |
| 51 | database_path: config.get("database_path")?, |
| 52 | server_address: config.get("server_address")?, |
| 53 | certificate_path: config.get("certificate_path")?, |
| 54 | key_path: config.get("key_path")?, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | fn sibling_directory(database_path: &str, name: &str) -> Result<PathBuf, ServerError> { |
| 59 | let directory = Path::new(database_path) |
| 60 | .parent() |
| 61 | .unwrap_or_else(|| Path::new("")) |
| 62 | .join(name); |
| 63 | std::fs::create_dir_all(&directory)?; |
| 64 | |
| 65 | Ok(directory) |
| 66 | } |
| 67 | |
| 68 | pub async fn showtime() -> Result<(), ServerError> { |
| 69 | let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); |
| 70 | |
| 71 | let config = load_config()?; |
| 72 | |
| 73 | let database = Db::open(&config.database_path).await?; |
| 74 | let certificate = certificate::load(&config.certificate_path, &config.key_path)?; |
| 75 | let attachments = sibling_directory(&config.database_path, "attachments")?; |
| 76 | let backups = sibling_directory(&config.database_path, "backups")?; |
| 77 | let updates = sibling_directory(&config.database_path, "updates")?; |
| 78 | tokio::spawn(files::collect(database.clone(), attachments.clone())); |
| 79 | |
| 80 | network::serve( |
| 81 | &config.server_address, |
| 82 | certificate, |
| 83 | database, |
| 84 | attachments, |
| 85 | backups, |
| 86 | updates, |
| 87 | ) |
| 88 | .await |
| 89 | } |
| 90 | |
| 91 | pub async fn set_role(username: &str, role: Role) -> Result<(), ServerError> { |
| 92 | let config = load_config()?; |
| 93 | let database = Db::open(&config.database_path).await?; |
| 94 | database |
| 95 | .run(async |db| { |
| 96 | let user = db::user::by_username(username, db) |
| 97 | .await? |
| 98 | .ok_or(CommonError::NotFound)?; |
| 99 | db::user::set_role_by_id(user.id, role, db).await?; |
| 100 | |
| 101 | Ok(()) |
| 102 | }) |
| 103 | .await |
| 104 | } |