From f6292407d036f8e5109f946c44619cd72f4befcb Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 2 Feb 2022 16:32:10 +0200 Subject: [PATCH] add save and restart option for config form --- peach-web/src/routes/settings/scuttlebutt.rs | 79 +++++++++++++------ .../scuttlebutt/configure_sbot.html.tera | 1 + 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/peach-web/src/routes/settings/scuttlebutt.rs b/peach-web/src/routes/settings/scuttlebutt.rs index e8c53a7..bf100ad 100644 --- a/peach-web/src/routes/settings/scuttlebutt.rs +++ b/peach-web/src/routes/settings/scuttlebutt.rs @@ -102,8 +102,12 @@ pub fn configure_sbot(flash: Option, _auth: Authenticated) -> Temp // fields to re-render forms with submitted values on error /// Receive the sbot configuration form data and save it to file. -#[post("/configure", data = "")] -pub fn configure_sbot_post(config: Form, _auth: Authenticated) -> Flash { +#[post("/configure?", data = "")] +pub fn configure_sbot_post( + restart: bool, + config: Form, + _auth: Authenticated, +) -> Flash { // call `into_inner()` to take ownership of the `config` data let owned_config = config.into_inner(); @@ -147,10 +151,26 @@ pub fn configure_sbot_post(config: Form, _auth: Authenticated) - // write config to file match SbotConfig::write(config) { - Ok(_) => Flash::success( - Redirect::to("/settings/scuttlebutt/configure"), - "Configuration updated. The pub must be (re)started for the changes to take effect", - ), + Ok(_) => { + // if `restart` query parameter is `true`, attempt sbot process (re)start + if restart { + restart_sbot_process( + // redirect url + "/settings/scuttlebutt/configure", + // success flash msg + "Updated configuration and restarted the sbot process", + // first failed flash msg + "Updated configuration but failed to start the sbot process", + // second failed flash msg + "Updated configuration but failed to stop the sbot process", + ) + } else { + Flash::success( + Redirect::to("/settings/scuttlebutt/configure"), + "Updated configuration", + ) + } + } Err(e) => Flash::error( Redirect::to("/settings/scuttlebutt/configure"), format!("Failed to update configuration: {}", e), @@ -217,25 +237,12 @@ pub fn stop_sbot(_auth: Authenticated) -> Flash { /// the attempt via a flash message. #[get("/restart")] pub fn restart_sbot(_auth: Authenticated) -> Flash { - info!("Restarting go-sbot.service"); - match systemctl_sbot_cmd("stop") { - // if stop was successful, try to start the process - Ok(_) => match systemctl_sbot_cmd("start") { - Ok(_) => Flash::success( - Redirect::to("/settings/scuttlebutt"), - "Sbot process has been restarted", - ), - - Err(_) => Flash::error( - Redirect::to("/settings/scuttlebutt"), - "Failed to start the sbot process", - ), - }, - Err(_) => Flash::error( - Redirect::to("/settings/scuttlebutt"), - "Failed to stop the sbot process", - ), - } + restart_sbot_process( + "/settings/scuttlebutt", + "Sbot process has been restarted", + "Failed to start the sbot process", + "Failed to stop the sbot process", + ) } // HELPER FUNCTIONS @@ -248,3 +255,25 @@ pub fn systemctl_sbot_cmd(cmd: &str) -> io::Result { .arg("go-sbot.service") .output() } + +/// Executes a systemctl stop command followed by start command. +/// Returns a redirect with a flash message stating the output of the restart attempt. +fn restart_sbot_process( + redirect_url: &str, + success_msg: &str, + start_failed_msg: &str, + stop_failed_msg: &str, +) -> Flash { + let url = redirect_url.to_string(); + + info!("Restarting go-sbot.service"); + match systemctl_sbot_cmd("stop") { + // if stop was successful, try to start the process + Ok(_) => match systemctl_sbot_cmd("start") { + Ok(_) => Flash::success(Redirect::to(url), success_msg), + + Err(e) => Flash::error(Redirect::to(url), format!("{}: {}", start_failed_msg, e)), + }, + Err(e) => Flash::error(Redirect::to(url), format!("{}: {}", stop_failed_msg, e)), + } +} diff --git a/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera b/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera index 94f846f..a791519 100644 --- a/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera +++ b/peach-web/templates/settings/scuttlebutt/configure_sbot.html.tera @@ -76,6 +76,7 @@ + Restore Defaults