Skip to content

Commit

Permalink
reloadable configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Jan 24, 2025
1 parent 1d02cc5 commit a9c5f71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/admin/server/commands.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,10 +23,26 @@ pub(super) async fn show_config(&self) -> Result<RoomMessageEventContent> {
// 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<PathBuf>,
) -> Result<RoomMessageEventContent> {
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,
Expand Down
7 changes: 7 additions & 0 deletions src/admin/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod commands;

use std::path::PathBuf;

use clap::Subcommand;
use conduwuit::Result;

Expand All @@ -14,6 +16,11 @@ pub(super) enum ServerCommand {
/// - Show configuration values
ShowConfig,

/// - Reload configuration values
ReloadConfig {
path: Option<PathBuf>,
},

/// - List the features built into the server
ListFeatures {
#[arg(short, long)]
Expand Down
6 changes: 3 additions & 3 deletions src/core/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Server {
#[must_use]
pub fn new(config: Config, runtime: Option<runtime::Handle>, log: Log) -> Self {
Self {
config,
config: config::Manager::new(config),
started: SystemTime::now(),
stopping: AtomicBool::new(false),
reloading: AtomicBool::new(false),
Expand Down

0 comments on commit a9c5f71

Please sign in to comment.