add power menu to settings menu and mount routes

This commit is contained in:
glyph 2022-11-28 09:10:42 +02:00
parent 7d5d6bcc1f
commit 8cbb295c3a
9 changed files with 106 additions and 10 deletions

View File

@ -49,10 +49,6 @@ pub fn mount_peachpub_routes(
Response::html(routes::guide::build_template())
},
(GET) (/power) => {
Response::html(routes::power::menu::build_template())
},
(POST) (/scuttlebutt/block) => {
routes::scuttlebutt::block::handle_form(request)
},
@ -170,6 +166,18 @@ pub fn mount_peachpub_routes(
routes::settings::admin::delete::handle_form(request)
},
(GET) (/settings/power) => {
Response::html(routes::settings::power::menu::build_template(request))
},
(GET) (/settings/power/reboot) => {
routes::settings::power::reboot::handle_reboot()
},
(GET) (/settings/power/shutdown) => {
routes::settings::power::shutdown::handle_shutdown()
},
(GET) (/settings/scuttlebutt) => {
Response::html(routes::settings::scuttlebutt::menu::build_template(request))
.reset_flash()

View File

@ -3,7 +3,6 @@ pub mod authentication;
//pub mod index;
pub mod guide;
pub mod home;
pub mod power;
pub mod scuttlebutt;
pub mod settings;
pub mod status;

View File

@ -1 +0,0 @@
pub mod menu;

View File

@ -11,8 +11,10 @@ pub fn build_template() -> PreEscaped<String> {
div class="card center" {
(PreEscaped("<!-- BUTTONS -->"))
div id="settingsButtons" {
// render the network settings button if we're not in standalone mode
// render the network settings and power menu buttons if we're
// not in standalone mode
@if !SERVER_CONFIG.standalone_mode {
a id="power" class="button button-primary center" href="/settings/power" title="Power Menu" { "Power" }
a id="network" class="button button-primary center" href="/settings/network" title="Network Settings" { "Network" }
}
a id="scuttlebutt" class="button button-primary center" href="/settings/scuttlebutt" title="Scuttlebutt Settings" { "Scuttlebutt" }

View File

@ -2,5 +2,6 @@ pub mod admin;
//pub mod dns;
pub mod menu;
pub mod network;
pub mod power;
pub mod scuttlebutt;
pub mod theme;

View File

@ -1,8 +1,17 @@
use maud::{html, PreEscaped};
use rouille::Request;
use crate::{templates, utils::theme};
use crate::{
templates,
utils::{flash::FlashRequest, theme},
};
/// Power menu template builder.
///
/// Presents options for rebooting or shutting down the device.
pub fn build_template(request: &Request) -> PreEscaped<String> {
let (flash_name, flash_msg) = request.retrieve_flash();
pub fn build_template() -> PreEscaped<String> {
let power_menu_template = html! {
(PreEscaped("<!-- POWER MENU -->"))
div class="card center" {
@ -10,7 +19,11 @@ pub fn build_template() -> PreEscaped<String> {
div id="buttons" {
a id="rebootBtn" class="button button-primary center" href="/reboot" title="Reboot Device" { "Reboot" }
a id="shutdownBtn" class="button button-warning center" href="/shutdown" title="Shutdown Device" { "Shutdown" }
a id="cancelBtn" class="button button-secondary center" href="/" title="Cancel" { "Cancel" }
a id="cancelBtn" class="button button-secondary center" href="/settings" title="Cancel" { "Cancel" }
}
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}

View File

@ -0,0 +1,3 @@
pub mod menu;
pub mod reboot;
pub mod shutdown;

View File

@ -0,0 +1,36 @@
use log::info;
use rouille::Response;
use std::{
io::Result,
process::{Command, Output},
};
use crate::utils::flash::FlashResponse;
/// Executes a system command to reboot the device immediately.
fn reboot() -> Result<Output> {
info!("Rebooting the device");
// ideally, we'd like to reboot after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "rebooting..." message to `peach-oled` for display
Command::new("sudo")
.arg("shutdown")
.arg("-r")
.arg("now")
.output()
}
pub fn handle_reboot() -> Response {
let (name, msg) = match reboot() {
Ok(_) => ("success".to_string(), "Rebooting the device".to_string()),
Err(err) => (
"error".to_string(),
format!("Failed to reboot the device: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/power").add_flash(flash_name, flash_msg)
}

View File

@ -0,0 +1,35 @@
use log::info;
use rouille::Response;
use std::{
io::Result,
process::{Command, Output},
};
use crate::utils::flash::FlashResponse;
/// Executes a system command to shutdown the device immediately.
fn shutdown() -> Result<Output> {
info!("Shutting down the device");
// ideally, we'd like to shutdown after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "shutting down..." message to `peach-oled` for display
Command::new("sudo").arg("shutdown").arg("now").output()
}
pub fn handle_shutdown() -> Response {
let (name, msg) = match shutdown() {
Ok(_) => (
"success".to_string(),
"Shutting down the device".to_string(),
),
Err(err) => (
"error".to_string(),
format!("Failed to shutdown the device: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/power").add_flash(flash_name, flash_msg)
}