add save and restart option for config form

This commit is contained in:
glyph 2022-02-02 16:32:10 +02:00
parent dfc173d941
commit f6292407d0
2 changed files with 55 additions and 25 deletions

View File

@ -102,8 +102,12 @@ pub fn configure_sbot(flash: Option<FlashMessage>, _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 = "<config>")]
pub fn configure_sbot_post(config: Form<SbotConfigForm>, _auth: Authenticated) -> Flash<Redirect> {
#[post("/configure?<restart>", data = "<config>")]
pub fn configure_sbot_post(
restart: bool,
config: Form<SbotConfigForm>,
_auth: Authenticated,
) -> Flash<Redirect> {
// 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<SbotConfigForm>, _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<Redirect> {
/// the attempt via a flash message.
#[get("/restart")]
pub fn restart_sbot(_auth: Authenticated) -> Flash<Redirect> {
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<Output> {
.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<Redirect> {
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)),
}
}

View File

@ -76,6 +76,7 @@
<input type="hidden" id="nounixsock" name="nounixsock" value="{{ sbot_config.nounixsock }}">
<!-- BUTTONS -->
<input id="saveConfig" class="button button-primary center" type="submit" title="Save configuration parameters to file" value="Save">
<input id="saveRestartConfig" class="button button-primary center" type="submit" title="Save configuration parameters to file and then (re)start the pub" value="Save & Restart" formaction="/settings/scuttlebutt/configure?restart=true">
<a id="restoreDefaults" class="button button-warning center" href="/settings/scuttlebutt/configure/default" title="Restore default configuration parameters and save them to file">Restore Defaults</a>
</form>
<!-- FLASH MESSAGE -->