forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

lib.rs112 lines, 3.0 KiBHistoryRawDownload
1pub mod authentication;
2pub mod backup;
3pub mod call;
4pub mod certificate;
5pub mod datagram;
6pub mod db;
7pub mod error;
8pub mod files;
9pub mod hub;
10pub mod network;
11pub mod publish;
12pub mod rate_limit;
13pub mod request;
14pub mod retention;
15pub mod rooms;
16pub mod stats;
17pub mod update;
18
19#[cfg(test)]
20mod test;
21
22use std::path::{Path, PathBuf};
23
24use common::{config::Config, error::Error as CommonError, model::Role};
25
26use crate::{db::Db, error::ServerError};
27
28const SERVER_CONFIG_FILE_PATH: &str = concat!(
29 env!("CARGO_MANIFEST_DIR"),
30 "/../../configs/server_config.toml"
31);
32
33fn 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
39pub struct ServerConfig {
40 pub database_path: String,
41 pub server_address: String,
42 pub certificate_path: String,
43 pub key_path: String,
44}
45
46fn 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
58fn 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
68pub async fn showtime() -> Result<network::Exit, ServerError> {
69 let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
70 update::remember_exe();
71
72 let config = load_config()?;
73
74 let database = Db::open(&config.database_path).await?;
75 let certificate = certificate::load(&config.certificate_path, &config.key_path)?;
76 let attachments = sibling_directory(&config.database_path, "attachments")?;
77 let backups = sibling_directory(&config.database_path, "backups")?;
78 let releases = update::Releases::load(
79 sibling_directory(&config.database_path, "clients")?,
80 &database,
81 )
82 .await?;
83 tokio::spawn(files::collect(database.clone(), attachments.clone()));
84
85 network::serve(
86 &config.server_address,
87 certificate,
88 database,
89 attachments,
90 backups,
91 releases,
92 )
93 .await
94}
95
96pub async fn set_role(username: &str, role: Role) -> Result<(), ServerError> {
97 let config = load_config()?;
98 let database = Db::open(&config.database_path).await?;
99 database
100 .run(async |db| {
101 let user = db::user::by_username(username, db)
102 .await?
103 .ok_or(CommonError::NotFound)?;
104 match role {
105 Role::Builder => db::user::make_builder(user.id, db).await?,
106 role => db::user::set_role_by_id(user.id, role, db).await?,
107 };
108
109 Ok(())
110 })
111 .await
112}