|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::sync::RwLock; |
| 4 | +use std::{fs, path::Path}; |
| 5 | +use thiserror::Error; |
| 6 | +use clap::ValueEnum; |
| 7 | + |
| 8 | +static CONFIG: once_cell::sync::OnceCell<RwLock<MisskeyConfig>> = once_cell::sync::OnceCell::new(); |
| 9 | + |
| 10 | +#[derive(Debug, Serialize, Deserialize)] |
| 11 | +#[serde(rename_all = "camelCase")] |
| 12 | +pub struct MisskeyConfig { |
| 13 | + pub publish_tarball_instead_of_provide_repository_url: Option<bool>, |
| 14 | + pub setup_password: Option<String>, |
| 15 | + pub url: String, |
| 16 | + pub port: u64, |
| 17 | + pub db: DbConfig, |
| 18 | + pub db_replications: bool, |
| 19 | + pub db_slaves: Option<Vec<DbConfig>>, |
| 20 | + pub redis: RedisConfig, |
| 21 | + pub redis_for_pubsub: Option<RedisConfig>, |
| 22 | + pub redis_for_job_queue: Option<RedisConfig>, |
| 23 | + pub redis_for_timelines: Option<RedisConfig>, |
| 24 | + pub redis_for_reactions: Option<RedisConfig>, |
| 25 | + #[serde(rename = "fulltextSearch")] |
| 26 | + pub full_text_search: Option<FullTextSearch>, |
| 27 | + pub meilisearch: Option<MeilisearchConfig>, |
| 28 | + pub id: IdMethod, |
| 29 | + pub serde_for_backend: Option<SentryForBackendConfig>, |
| 30 | + pub serde_for_frontend: Option<SentryForFrontendConfig>, |
| 31 | + pub disable_hsts: Option<bool>, |
| 32 | + pub cluster_limit: Option<u64>, |
| 33 | + pub deliver_job_concurrency: Option<u64>, |
| 34 | + pub inbox_job_concurrency: Option<u64>, |
| 35 | + pub relationship_job_concurrency: Option<u64>, |
| 36 | + pub deliver_job_per_sec: Option<u64>, |
| 37 | + pub inbox_job_per_sec: Option<u64>, |
| 38 | + pub relationship_job_per_sec: Option<u64>, |
| 39 | + pub deliver_job_max_attempts: Option<u64>, |
| 40 | + pub inbox_job_max_attempts: Option<u64>, |
| 41 | + pub outgoing_address: Option<String>, |
| 42 | + pub outgoing_address_family: Option<OutgoingAddressFamily>, |
| 43 | + pub proxy: Option<String>, |
| 44 | + pub proxy_bypass_hosts: Option<Vec<String>>, |
| 45 | + pub proxy_smtp: Option<String>, |
| 46 | + pub media_proxy: Option<String>, |
| 47 | + pub proxy_remote_files: bool, |
| 48 | + pub video_thumbnail_generator: Option<String>, |
| 49 | + pub sign_to_activity_pub_get: bool, |
| 50 | + pub allowed_private_networks: Option<Vec<String>>, |
| 51 | + pub max_file_size: Option<u64>, |
| 52 | + pub pid_file: Option<String>, |
| 53 | + pub per_channel_max_note_cache_count: Option<u64>, |
| 54 | + pub per_user_notifications_max_count: Option<u64>, |
| 55 | + pub deactivate_antenna_threshold: Option<u64>, |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Debug, Serialize, Deserialize)] |
| 59 | +pub struct DbConfig { |
| 60 | + pub host: String, |
| 61 | + pub port: u64, |
| 62 | + pub db: String, |
| 63 | + pub user: String, |
| 64 | + pub pass: String, |
| 65 | + pub disable_cache: Option<bool>, |
| 66 | + pub extra: Option<DbExtraConfig>, |
| 67 | +} |
| 68 | + |
| 69 | +// TODO: pgのextraオプションへの追加対応 https://github.com/misskey-dev/misskey/issues/15108 |
| 70 | +#[derive(Debug, Serialize, Deserialize)] |
| 71 | +pub struct DbExtraConfig { |
| 72 | + pub ssl: bool, |
| 73 | + pub statement_timeout: Option<u64>, |
| 74 | + pub query_timeout: Option<u64>, |
| 75 | + pub lock_timeout: Option<u64>, |
| 76 | + #[serde(flatten)] |
| 77 | + #[serde(skip_serializing)] |
| 78 | + _ignored: HashMap<String, serde_yml::Value>, |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug, Serialize, Deserialize)] |
| 82 | +pub struct RedisConfig { |
| 83 | + pub host: String, |
| 84 | + pub port: u64, |
| 85 | + pub family: Option<RedisFamily>, |
| 86 | + pub pass: Option<String>, |
| 87 | + pub prefix: Option<String>, |
| 88 | + pub db: Option<u64>, |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Debug, Serialize, Deserialize)] |
| 92 | +pub enum RedisFamily { |
| 93 | + Both = 0, |
| 94 | + IPv4 = 4, |
| 95 | + IPv6 = 6, |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Debug, Serialize, Deserialize, Clone, Copy)] |
| 99 | +pub struct FullTextSearch { |
| 100 | + pub provider: FullTextSearchProvider, |
| 101 | +} |
| 102 | + |
| 103 | +// TODO: サメがtsvectorを使う場合の設定も追加 |
| 104 | +#[derive(Debug, Serialize, Deserialize, Clone, Copy, ValueEnum)] |
| 105 | +#[serde(rename_all = "camelCase")] |
| 106 | +pub enum FullTextSearchProvider { |
| 107 | + #[serde(rename = "sqlLike")] |
| 108 | + SqlLike, |
| 109 | + #[serde(rename = "sqlPgroonga")] |
| 110 | + SqlPgroonga, |
| 111 | + #[serde(rename = "meilisearch")] |
| 112 | + Meilisearch, |
| 113 | +} |
| 114 | + |
| 115 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 116 | +#[serde(rename_all = "camelCase")] |
| 117 | +pub struct MeilisearchConfig { |
| 118 | + pub host: String, |
| 119 | + pub port: u64, |
| 120 | + pub api_key: String, |
| 121 | + pub ssl: bool, |
| 122 | + pub index: String, |
| 123 | + pub scope: Option<MeilisearchScope>, |
| 124 | +} |
| 125 | + |
| 126 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
| 127 | +#[serde(rename_all = "lowercase")] |
| 128 | +pub enum MeilisearchScope { |
| 129 | + Local, |
| 130 | + Global, |
| 131 | + Custom(Vec<String>), |
| 132 | +} |
| 133 | + |
| 134 | +#[derive(Debug, Default, Serialize, Deserialize, Clone, Copy, ValueEnum)] |
| 135 | +#[serde(rename_all = "lowercase")] |
| 136 | +pub enum IdMethod { |
| 137 | + Aid, |
| 138 | + #[default] |
| 139 | + Aidx, |
| 140 | + Meid, |
| 141 | + Ulid, |
| 142 | + ObjectId, |
| 143 | +} |
| 144 | + |
| 145 | +#[derive(Debug, Serialize, Deserialize)] |
| 146 | +#[serde(rename_all = "camelCase")] |
| 147 | +pub struct SentryForBackendConfig { |
| 148 | + pub enable_node_profiling: bool, |
| 149 | + pub options: Option<SentryOptions>, |
| 150 | +} |
| 151 | + |
| 152 | +#[derive(Debug, Serialize, Deserialize)] |
| 153 | +#[serde(rename_all = "camelCase")] |
| 154 | +pub struct SentryForFrontendConfig { |
| 155 | + pub options: Option<SentryOptions>, |
| 156 | +} |
| 157 | + |
| 158 | +#[derive(Debug, Serialize, Deserialize)] |
| 159 | +pub struct SentryOptions { |
| 160 | + pub dsn: String, |
| 161 | +} |
| 162 | + |
| 163 | +#[derive(Debug, Serialize, Deserialize)] |
| 164 | +#[serde(rename_all = "lowercase")] |
| 165 | +pub enum OutgoingAddressFamily { |
| 166 | + IPv4, |
| 167 | + IPv6, |
| 168 | + Dual, |
| 169 | +} |
| 170 | + |
| 171 | +#[derive(Debug, Error)] |
| 172 | +pub enum ConfigError { |
| 173 | + #[error("Config not initialized")] |
| 174 | + ConfigNotInitialized, |
| 175 | + |
| 176 | + #[error("File not found: {path}")] |
| 177 | + ConfigFileNotFound { path: String }, |
| 178 | + |
| 179 | + #[error("Failed to read config file: {0}")] |
| 180 | + ConfigFileReadError(#[from] std::io::Error), |
| 181 | + |
| 182 | + #[error("Failed to parse config file: {0}")] |
| 183 | + ConfigFileParseError(#[from] serde_yml::Error), |
| 184 | + |
| 185 | + #[error("Validation error: {0}")] |
| 186 | + ConfigValidationError(String), |
| 187 | + |
| 188 | + #[error("Initialization error")] |
| 189 | + ConfigInitializationError, |
| 190 | +} |
| 191 | + |
| 192 | +pub struct ServerConfig; |
| 193 | + |
| 194 | +impl ServerConfig { |
| 195 | + pub fn init(config_path: &str) -> Result<(), ConfigError> { |
| 196 | + let config = Self::load_from_file(config_path)?; |
| 197 | + |
| 198 | + CONFIG |
| 199 | + .set(RwLock::new(config)) |
| 200 | + .map_err(|_| ConfigError::ConfigInitializationError); |
| 201 | + |
| 202 | + Ok(()) |
| 203 | + } |
| 204 | + |
| 205 | + fn load_from_file(config_path: &str) -> Result<MisskeyConfig, ConfigError> { |
| 206 | + let path = Path::new(config_path); |
| 207 | + |
| 208 | + if !path.exists() { |
| 209 | + return Err(ConfigError::ConfigFileNotFound { |
| 210 | + path: config_path.to_string(), |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + let config_content = match fs::read_to_string(path) { |
| 215 | + Ok(content) => content, |
| 216 | + Err(e) => return Err(ConfigError::ConfigFileReadError(e)), |
| 217 | + }; |
| 218 | + let config: MisskeyConfig = match serde_yml::from_str(&config_content) { |
| 219 | + Ok(cfg) => cfg, |
| 220 | + Err(e) => return Err(ConfigError::ConfigFileParseError(e)), |
| 221 | + }; |
| 222 | + |
| 223 | + Ok(config) |
| 224 | + } |
| 225 | + |
| 226 | + pub fn get() -> Result<std::sync::RwLockReadGuard<'static, MisskeyConfig>, ConfigError> { |
| 227 | + let config = CONFIG.get().ok_or(ConfigError::ConfigNotInitialized)?; |
| 228 | + config.read().map_err(|_| ConfigError::ConfigNotInitialized) |
| 229 | + } |
| 230 | + |
| 231 | + pub fn get_id_method() -> Result<IdMethod, ConfigError> { |
| 232 | + Self::get().map(|cfg| cfg.id) |
| 233 | + } |
| 234 | + |
| 235 | + pub fn get_search_provider() -> Result<Option<FullTextSearchProvider>, ConfigError> { |
| 236 | + Self::get().map(|cfg| cfg.full_text_search.map(|provider| provider.provider)) |
| 237 | + } |
| 238 | + |
| 239 | + pub fn get_meilisearch_config() -> Result<Option<MeilisearchConfig>, ConfigError> { |
| 240 | + Self::get().map(|cfg| cfg.meilisearch.clone()) |
| 241 | + } |
| 242 | +} |
0 commit comments