From a9c5f71b7523009b5e9bd9a8b6c3411bdfa18f6b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 24 Jan 2025 07:04:29 +0000 Subject: [PATCH] reloadable configuration Signed-off-by: Jason Volk --- src/admin/server/commands.rs | 22 +++++++++++++++++++--- src/admin/server/mod.rs | 7 +++++++ src/core/server.rs | 6 +++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/admin/server/commands.rs b/src/admin/server/commands.rs index 6469a0e95..3ea278830 100644 --- a/src/admin/server/commands.rs +++ b/src/admin/server/commands.rs @@ -1,6 +1,6 @@ -use std::{fmt::Write, sync::Arc}; +use std::{fmt::Write, path::PathBuf, sync::Arc}; -use conduwuit::{info, utils::time, warn, Err, Result}; +use conduwuit::{info, utils::time, warn, Config, Err, Result}; use ruma::events::room::message::RoomMessageEventContent; use crate::admin_command; @@ -23,10 +23,26 @@ pub(super) async fn show_config(&self) -> Result { // Construct and send the response Ok(RoomMessageEventContent::text_markdown(format!( "{}", - self.services.server.config + *self.services.server.config ))) } +#[admin_command] +pub(super) async fn reload_config( + &self, + path: Option, +) -> Result { + let path = path.as_deref().into_iter(); + let config = Config::load(path).and_then(|raw| Config::new(&raw))?; + if config.server_name != self.services.server.config.server_name { + return Err!("You can't change the server name."); + } + + let _old = self.services.server.config.update(config)?; + + Ok(RoomMessageEventContent::text_plain("Successfully reconfigured.")) +} + #[admin_command] pub(super) async fn list_features( &self, diff --git a/src/admin/server/mod.rs b/src/admin/server/mod.rs index 69ad7953f..3f3d6c5e5 100644 --- a/src/admin/server/mod.rs +++ b/src/admin/server/mod.rs @@ -1,5 +1,7 @@ mod commands; +use std::path::PathBuf; + use clap::Subcommand; use conduwuit::Result; @@ -14,6 +16,11 @@ pub(super) enum ServerCommand { /// - Show configuration values ShowConfig, + /// - Reload configuration values + ReloadConfig { + path: Option, + }, + /// - List the features built into the server ListFeatures { #[arg(short, long)] diff --git a/src/core/server.rs b/src/core/server.rs index 948eea363..6838c9c91 100644 --- a/src/core/server.rs +++ b/src/core/server.rs @@ -8,12 +8,12 @@ use std::{ use tokio::{runtime, sync::broadcast}; -use crate::{config::Config, err, log::Log, metrics::Metrics, Err, Result}; +use crate::{config, config::Config, err, log::Log, metrics::Metrics, Err, Result}; /// Server runtime state; public portion pub struct Server { /// Server-wide configuration instance - pub config: Config, + pub config: config::Manager, /// Timestamp server was started; used for uptime. pub started: SystemTime, @@ -46,7 +46,7 @@ impl Server { #[must_use] pub fn new(config: Config, runtime: Option, log: Log) -> Self { Self { - config, + config: config::Manager::new(config), started: SystemTime::now(), stopping: AtomicBool::new(false), reloading: AtomicBool::new(false),