reorganise routes

This commit is contained in:
glyph 2021-11-15 17:32:00 +02:00
parent c73287cc27
commit 8baf68715d
12 changed files with 210 additions and 155 deletions

View File

@ -1,7 +1,7 @@
use log::debug; use log::debug;
use rocket::{catch}; use rocket::catch;
use rocket_dyn_templates::Template;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_dyn_templates::Template;
use serde::Serialize; use serde::Serialize;
// HELPERS AND ROUTES FOR 404 ERROR // HELPERS AND ROUTES FOR 404 ERROR
@ -34,7 +34,7 @@ pub fn not_found() -> Template {
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = Some("No resource found for given URL".to_string()); context.flash_msg = Some("No resource found for given URL".to_string());
Template::render("not_found", context) Template::render("catchers/not_found", context)
} }
// HELPERS AND ROUTES FOR 500 ERROR // HELPERS AND ROUTES FOR 500 ERROR
@ -48,7 +48,7 @@ pub fn internal_error() -> Template {
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = Some("Internal server error".to_string()); context.flash_msg = Some("Internal server error".to_string());
Template::render("internal_error", context) Template::render("catchers/internal_error", context)
} }
// HELPERS AND ROUTES FOR 403 FORBIDDEN // HELPERS AND ROUTES FOR 403 FORBIDDEN
@ -57,4 +57,4 @@ pub fn internal_error() -> Template {
pub fn forbidden() -> Redirect { pub fn forbidden() -> Redirect {
debug!("403 Forbidden"); debug!("403 Forbidden");
Redirect::to("/login") Redirect::to("/login")
} }

View File

@ -24,13 +24,13 @@ impl HomeContext {
} }
#[get("/")] #[get("/")]
pub fn index(_auth: Authenticated) -> Template { pub fn home(_auth: Authenticated) -> Template {
let context = HomeContext { let context = HomeContext {
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
title: None, title: None,
}; };
Template::render("index", &context) Template::render("home", &context)
} }
// HELPERS AND ROUTES FOR /help // HELPERS AND ROUTES FOR /help

View File

@ -1,7 +1,6 @@
pub mod authentication; pub mod authentication;
pub mod device;
pub mod catchers; pub mod catchers;
pub mod index; pub mod index;
pub mod ping;
pub mod scuttlebutt; pub mod scuttlebutt;
pub mod settings; pub mod settings;
pub mod status;

View File

@ -45,7 +45,7 @@ pub fn private(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("messages", &context) Template::render("scuttlebutt/messages", &context)
} }
// HELPERS AND ROUTES FOR /peers // HELPERS AND ROUTES FOR /peers
@ -81,7 +81,7 @@ pub fn peers(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("peers", &context) Template::render("scuttlebutt/peers", &context)
} }
// HELPERS AND ROUTES FOR /post/publish // HELPERS AND ROUTES FOR /post/publish
@ -209,7 +209,7 @@ pub fn profile(
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("profile", &context) Template::render("scuttlebutt/profile", &context)
} }
// HELPERS AND ROUTES FOR /friends // HELPERS AND ROUTES FOR /friends
@ -247,7 +247,7 @@ pub fn friends(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("peers_list", &context) Template::render("scuttlebutt/peers_list", &context)
} }
// HELPERS AND ROUTES FOR /follows // HELPERS AND ROUTES FOR /follows
@ -285,7 +285,7 @@ pub fn follows(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("peers_list", &context) Template::render("scuttlebutt/peers_list", &context)
} }
// HELPERS AND ROUTES FOR /followers // HELPERS AND ROUTES FOR /followers
@ -323,7 +323,7 @@ pub fn followers(flash: Option<FlashMessage>, _auth: Authenticated) -> Template
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("peers_list", &context) Template::render("scuttlebutt/peers_list", &context)
} }
// HELPERS AND ROUTES FOR /blocks // HELPERS AND ROUTES FOR /blocks
@ -361,5 +361,5 @@ pub fn blocks(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("peers_list", &context) Template::render("scuttlebutt/peers_list", &context)
} }

View File

@ -1,12 +1,12 @@
use rocket::serde::{Deserialize, Serialize};
use rocket::{ use rocket::{
form::{Form, FromForm},
get, post, get, post,
request::FlashMessage, request::FlashMessage,
form::{Form, FromForm},
response::{Flash, Redirect}, response::{Flash, Redirect},
uri, uri,
}; };
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use rocket::serde::{Deserialize, Serialize};
use peach_lib::config_manager; use peach_lib::config_manager;
use peach_lib::config_manager::load_peach_config; use peach_lib::config_manager::load_peach_config;
@ -39,12 +39,12 @@ impl ConfigureAdminContext {
} }
} }
/// View and delete currently configured admin. /// Administrator settings menu. View and delete currently configured admin.
#[get("/settings/configure_admin")] #[get("/")]
pub fn configure_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn configure_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = ConfigureAdminContext::build(); let mut context = ConfigureAdminContext::build();
// set back icon link to network route // set back icon link to settings route
context.back = Some("/network".to_string()); context.back = Some("/settings".to_string());
context.title = Some("Configure Admin".to_string()); context.title = Some("Configure Admin".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -83,14 +83,14 @@ impl AddAdminContext {
pub fn save_add_admin_form(admin_form: AddAdminForm) -> Result<(), PeachWebError> { pub fn save_add_admin_form(admin_form: AddAdminForm) -> Result<(), PeachWebError> {
let _result = config_manager::add_ssb_admin_id(&admin_form.ssb_id)?; let _result = config_manager::add_ssb_admin_id(&admin_form.ssb_id)?;
// if the previous line didn't throw an error then it was a success // if the previous line didn't throw an error then it was a success
Ok(()) Ok(())
} }
#[get("/settings/admin/add")] #[get("/add")]
pub fn add_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn add_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = AddAdminContext::build(); let mut context = AddAdminContext::build();
context.back = Some("/settings/configure_admin".to_string()); context.back = Some("/settings/admin".to_string());
context.title = Some("Add Admin".to_string()); context.title = Some("Add Admin".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -102,7 +102,7 @@ pub fn add_admin(flash: Option<FlashMessage>, _auth: Authenticated) -> Template
Template::render("admin/add_admin", &context) Template::render("admin/add_admin", &context)
} }
#[post("/settings/admin/add", data = "<add_admin_form>")] #[post("/add", data = "<add_admin_form>")]
pub fn add_admin_post(add_admin_form: Form<AddAdminForm>, _auth: Authenticated) -> Flash<Redirect> { pub fn add_admin_post(add_admin_form: Form<AddAdminForm>, _auth: Authenticated) -> Flash<Redirect> {
let result = save_add_admin_form(add_admin_form.into_inner()); let result = save_add_admin_form(add_admin_form.into_inner());
let url = uri!(configure_admin); let url = uri!(configure_admin);
@ -119,8 +119,11 @@ pub struct DeleteAdminForm {
pub ssb_id: String, pub ssb_id: String,
} }
#[post("/settings/admin/delete", data = "<delete_admin_form>")] #[post("/delete", data = "<delete_admin_form>")]
pub fn delete_admin_post(delete_admin_form: Form<DeleteAdminForm>, _auth: Authenticated) -> Flash<Redirect> { pub fn delete_admin_post(
delete_admin_form: Form<DeleteAdminForm>,
_auth: Authenticated,
) -> Flash<Redirect> {
let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id); let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id);
let url = uri!(configure_admin); let url = uri!(configure_admin);
match result { match result {

View File

@ -1,12 +1,14 @@
use log::info; use log::info;
use rocket::{ use rocket::{
form::{Form, FromForm},
get, post, get, post,
request::FlashMessage, request::FlashMessage,
form::{Form, FromForm} serde::{
json::{Json, Value},
Deserialize, Serialize,
},
}; };
use rocket::serde::json::Json;
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use rocket::serde::{Deserialize, Serialize};
use peach_lib::config_manager; use peach_lib::config_manager;
use peach_lib::config_manager::load_peach_config; use peach_lib::config_manager::load_peach_config;
@ -22,7 +24,6 @@ use peach_lib::jsonrpc_core::types::error::ErrorCode;
use crate::error::PeachWebError; use crate::error::PeachWebError;
use crate::routes::authentication::Authenticated; use crate::routes::authentication::Authenticated;
use crate::utils::build_json_response; use crate::utils::build_json_response;
use rocket::serde::json::Value;
#[derive(Debug, Deserialize, FromForm)] #[derive(Debug, Deserialize, FromForm)]
pub struct DnsForm { pub struct DnsForm {
@ -113,11 +114,11 @@ impl ConfigureDNSContext {
} }
} }
#[get("/network/dns")] #[get("/dns")]
pub fn configure_dns(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn configure_dns(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = ConfigureDNSContext::build(); let mut context = ConfigureDNSContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("Configure DNS".to_string()); context.title = Some("Configure DNS".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -125,35 +126,35 @@ pub fn configure_dns(flash: Option<FlashMessage>, _auth: Authenticated) -> Templ
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("configure_dns", &context) Template::render("settings/network/configure_dns", &context)
} }
#[post("/network/dns", data = "<dns>")] #[post("/dns", data = "<dns>")]
pub fn configure_dns_post(dns: Form<DnsForm>, _auth: Authenticated) -> Template { pub fn configure_dns_post(dns: Form<DnsForm>, _auth: Authenticated) -> Template {
let result = save_dns_configuration(dns.into_inner()); let result = save_dns_configuration(dns.into_inner());
match result { match result {
Ok(_) => { Ok(_) => {
let mut context = ConfigureDNSContext::build(); let mut context = ConfigureDNSContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("Configure DNS".to_string()); context.title = Some("Configure DNS".to_string());
context.flash_name = Some("success".to_string()); context.flash_name = Some("success".to_string());
context.flash_msg = Some("New dynamic dns configuration is now enabled".to_string()); context.flash_msg = Some("New dynamic dns configuration is now enabled".to_string());
Template::render("configure_dns", &context) Template::render("settings/network/configure_dns", &context)
} }
Err(err) => { Err(err) => {
let mut context = ConfigureDNSContext::build(); let mut context = ConfigureDNSContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("Configure DNS".to_string()); context.title = Some("Configure DNS".to_string());
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = Some(format!("Failed to save dns configurations: {}", err)); context.flash_msg = Some(format!("Failed to save dns configurations: {}", err));
Template::render("configure_dns", &context) Template::render("settings/network/configure_dns", &context)
} }
} }
} }
#[post("/api/v1/dns/configure", data = "<dns_form>")] #[post("/dns/configure", data = "<dns_form>")]
pub fn save_dns_configuration_endpoint(dns_form: Json<DnsForm>, _auth: Authenticated) -> Value { pub fn save_dns_configuration_endpoint(dns_form: Json<DnsForm>, _auth: Authenticated) -> Value {
let result = save_dns_configuration(dns_form.into_inner()); let result = save_dns_configuration(dns_form.into_inner());
match result { match result {

View File

@ -0,0 +1,41 @@
use rocket::{get, request::FlashMessage, serde::Serialize};
use rocket_dyn_templates::Template;
use crate::routes::authentication::Authenticated;
// HELPERS AND ROUTES FOR /settings
#[derive(Debug, Serialize)]
pub struct SettingsMenuContext {
pub back: Option<String>,
pub title: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
}
impl SettingsMenuContext {
pub fn build() -> SettingsMenuContext {
SettingsMenuContext {
back: None,
title: None,
flash_name: None,
flash_msg: None,
}
}
}
/// View and delete currently configured admin.
#[get("/settings")]
pub fn settings_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = SettingsMenuContext::build();
// set back icon link to network route
context.back = Some("/".to_string());
context.title = Some("Settings".to_string());
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("settings/menu", &context)
}

View File

@ -1,3 +1,4 @@
pub mod admin; pub mod admin;
pub mod dns; pub mod dns;
pub mod network; pub mod menu;
pub mod network;

View File

@ -1,27 +1,27 @@
use log::{debug, warn}; use log::{debug, warn};
use rocket::{ use rocket::{
get,
post,
request::FlashMessage,
form::{Form, FromForm}, form::{Form, FromForm},
get, post,
request::FlashMessage,
response::{Flash, Redirect}, response::{Flash, Redirect},
serde::{
json::{json, Json, Value},
Deserialize, Serialize,
},
uri, UriDisplayQuery, uri, UriDisplayQuery,
}; };
use rocket::serde::json::{json, Json};
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use rocket::serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use peach_lib::network_client; use peach_lib::network_client;
use peach_lib::network_client::{AccessPoint, Networks, Scan}; use peach_lib::network_client::{AccessPoint, Networks, Scan};
use peach_lib::stats_client::Traffic; use peach_lib::stats_client::Traffic;
use crate::routes::authentication::Authenticated;
use crate::utils::build_json_response;
use crate::utils::monitor; use crate::utils::monitor;
use crate::utils::monitor::{Alert, Data, Threshold}; use crate::utils::monitor::{Alert, Data, Threshold};
use crate::utils::build_json_response;
use crate::routes::authentication::Authenticated;
use rocket::serde::json::Value;
// STRUCTS USED BY NETWORK ROUTES // STRUCTS USED BY NETWORK ROUTES
@ -36,9 +36,9 @@ pub struct WiFi {
pub pass: String, pub pass: String,
} }
// HELPERS AND ROUTES FOR /network/wifi/usage/reset // HELPERS AND ROUTES FOR /settings/network/wifi/usage/reset
#[get("/network/wifi/usage/reset")] #[get("/wifi/usage/reset")]
pub fn wifi_usage_reset(_auth: Authenticated) -> Flash<Redirect> { pub fn wifi_usage_reset(_auth: Authenticated) -> Flash<Redirect> {
let url = uri!(wifi_usage); let url = uri!(wifi_usage);
match monitor::reset_data() { match monitor::reset_data() {
@ -50,7 +50,7 @@ pub fn wifi_usage_reset(_auth: Authenticated) -> Flash<Redirect> {
} }
} }
#[post("/network/wifi/connect", data = "<network>")] #[post("/wifi/connect", data = "<network>")]
pub fn connect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> { pub fn connect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid; let ssid = &network.ssid;
let url = uri!(network_detail(ssid = ssid)); let url = uri!(network_detail(ssid = ssid));
@ -63,7 +63,7 @@ pub fn connect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect
} }
} }
#[post("/network/wifi/disconnect", data = "<network>")] #[post("/wifi/disconnect", data = "<network>")]
pub fn disconnect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> { pub fn disconnect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid; let ssid = &network.ssid;
let url = uri!(network_home); let url = uri!(network_home);
@ -73,7 +73,7 @@ pub fn disconnect_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redir
} }
} }
#[post("/network/wifi/forget", data = "<network>")] #[post("/wifi/forget", data = "<network>")]
pub fn forget_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> { pub fn forget_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &network.ssid; let ssid = &network.ssid;
let url = uri!(network_home); let url = uri!(network_home);
@ -86,10 +86,10 @@ pub fn forget_wifi(network: Form<Ssid>, _auth: Authenticated) -> Flash<Redirect>
} }
} }
#[get("/network/wifi/modify?<ssid>")] #[get("/wifi/modify?<ssid>")]
pub fn wifi_password(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn wifi_password(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkAddContext { let mut context = NetworkAddContext {
back: Some("/network/wifi".to_string()), back: Some("/settings/network/wifi".to_string()),
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
selected: Some(ssid.to_string()), selected: Some(ssid.to_string()),
@ -102,10 +102,10 @@ pub fn wifi_password(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticat
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_modify", &context) Template::render("settings/network/network_modify", &context)
} }
#[post("/network/wifi/modify", data = "<wifi>")] #[post("/wifi/modify", data = "<wifi>")]
pub fn wifi_set_password(wifi: Form<WiFi>, _auth: Authenticated) -> Flash<Redirect> { pub fn wifi_set_password(wifi: Form<WiFi>, _auth: Authenticated) -> Flash<Redirect> {
let ssid = &wifi.ssid; let ssid = &wifi.ssid;
let pass = &wifi.pass; let pass = &wifi.pass;
@ -119,7 +119,7 @@ pub fn wifi_set_password(wifi: Form<WiFi>, _auth: Authenticated) -> Flash<Redire
} }
} }
// HELPERS AND ROUTES FOR /network // HELPERS AND ROUTES FOR /settings/network
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct NetworkContext { pub struct NetworkContext {
@ -273,12 +273,12 @@ impl NetworkContext {
} }
} }
#[get("/network")] #[get("/")]
pub fn network_home(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn network_home(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call // assign context through context_builder call
let mut context = NetworkContext::build(); let mut context = NetworkContext::build();
// set back button (nav) url // set back button (nav) url
context.back = Some("/".to_string()); context.back = Some("/settings".to_string());
// set page title // set page title
context.title = Some("Network Configuration".to_string()); context.title = Some("Network Configuration".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
@ -288,25 +288,28 @@ pub fn network_home(flash: Option<FlashMessage>, _auth: Authenticated) -> Templa
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_card", &context) Template::render("settings/network/network_card", &context)
} }
// HELPERS AND ROUTES FOR /network/ap/activate // HELPERS AND ROUTES FOR /settings/network/ap/activate
#[get("/network/ap/activate")] #[get("/ap/activate")]
pub fn deploy_ap(_auth: Authenticated) -> Flash<Redirect> { pub fn deploy_ap(_auth: Authenticated) -> Flash<Redirect> {
// activate the wireless access point // activate the wireless access point
debug!("Activating WiFi access point."); debug!("Activating WiFi access point.");
match network_client::activate_ap() { match network_client::activate_ap() {
Ok(_) => Flash::success(Redirect::to("/network"), "Activated WiFi access point"), Ok(_) => Flash::success(
Redirect::to("/settings/network"),
"Activated WiFi access point",
),
Err(_) => Flash::error( Err(_) => Flash::error(
Redirect::to("/network"), Redirect::to("/settings/network"),
"Failed to activate WiFi access point", "Failed to activate WiFi access point",
), ),
} }
} }
// HELPERS AND ROUTES FOR /network/wifi // HELPERS AND ROUTES FOR /settings/network/wifi
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct NetworkListContext { pub struct NetworkListContext {
@ -375,11 +378,11 @@ impl NetworkListContext {
} }
} }
#[get("/network/wifi")] #[get("/wifi")]
pub fn wifi_list(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn wifi_list(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call // assign context through context_builder call
let mut context = NetworkListContext::build(); let mut context = NetworkListContext::build();
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("WiFi Networks".to_string()); context.title = Some("WiFi Networks".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -388,10 +391,10 @@ pub fn wifi_list(flash: Option<FlashMessage>, _auth: Authenticated) -> Template
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_list", &context) Template::render("settings/network/network_list", &context)
} }
// HELPERS AND ROUTES FOR /network/wifi<ssid> // HELPERS AND ROUTES FOR /settings/network/wifi<ssid>
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct NetworkDetailContext { pub struct NetworkDetailContext {
@ -540,11 +543,11 @@ impl NetworkDetailContext {
} }
} }
#[get("/network/wifi?<ssid>")] #[get("/wifi?<ssid>")]
pub fn network_detail(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn network_detail(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call // assign context through context_builder call
let mut context = NetworkDetailContext::build(); let mut context = NetworkDetailContext::build();
context.back = Some("/network/wifi".to_string()); context.back = Some("/settings/network/wifi".to_string());
context.title = Some("WiFi Network".to_string()); context.title = Some("WiFi Network".to_string());
context.selected = Some(ssid.to_string()); context.selected = Some(ssid.to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
@ -554,28 +557,31 @@ pub fn network_detail(ssid: &str, flash: Option<FlashMessage>, _auth: Authentica
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_detail", &context) Template::render("settings/network/network_detail", &context)
} }
// HELPERS AND ROUTES FOR /network/wifi/activate // HELPERS AND ROUTES FOR /settings/network/wifi/activate
#[get("/network/wifi/activate")] #[get("/wifi/activate")]
pub fn deploy_client(_auth: Authenticated) -> Flash<Redirect> { pub fn deploy_client(_auth: Authenticated) -> Flash<Redirect> {
// activate the wireless client // activate the wireless client
debug!("Activating WiFi client mode."); debug!("Activating WiFi client mode.");
match network_client::activate_client() { match network_client::activate_client() {
Ok(_) => Flash::success(Redirect::to("/network"), "Activated WiFi client"), Ok(_) => Flash::success(Redirect::to("/settings/network"), "Activated WiFi client"),
Err(_) => Flash::error(Redirect::to("/network"), "Failed to activate WiFi client"), Err(_) => Flash::error(
Redirect::to("/settings/network"),
"Failed to activate WiFi client",
),
} }
} }
// HELPERS AND ROUTES FOR /network/wifi/add // HELPERS AND ROUTES FOR /settings/network/wifi/add
#[get("/network/wifi/add")] #[get("/wifi/add")]
pub fn network_add_wifi(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn add_wifi(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkContext::build(); let mut context = NetworkContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("Add WiFi Network".to_string()); context.title = Some("Add WiFi Network".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -584,10 +590,10 @@ pub fn network_add_wifi(flash: Option<FlashMessage>, _auth: Authenticated) -> Te
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_add", &context) Template::render("settings/network/network_add", &context)
} }
// used in /network/wifi/add?<ssid> // used in /settings/network/wifi/add?<ssid>
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct NetworkAddContext { pub struct NetworkAddContext {
pub back: Option<String>, pub back: Option<String>,
@ -609,10 +615,10 @@ impl NetworkAddContext {
} }
} }
#[get("/network/wifi/add?<ssid>")] #[get("/wifi/add?<ssid>")]
pub fn network_add_ssid(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn add_ssid(ssid: &str, flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkAddContext::build(); let mut context = NetworkAddContext::build();
context.back = Some("/network/wifi".to_string()); context.back = Some("/settings/network/wifi".to_string());
context.selected = Some(ssid.to_string()); context.selected = Some(ssid.to_string());
context.title = Some("Add WiFi Network".to_string()); context.title = Some("Add WiFi Network".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
@ -622,10 +628,10 @@ pub fn network_add_ssid(ssid: &str, flash: Option<FlashMessage>, _auth: Authenti
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_add", &context) Template::render("settings/network/network_add", &context)
} }
#[post("/network/wifi/add", data = "<wifi>")] #[post("/wifi/add", data = "<wifi>")]
pub fn add_credentials(wifi: Form<WiFi>, _auth: Authenticated) -> Template { pub fn add_credentials(wifi: Form<WiFi>, _auth: Authenticated) -> Template {
// check if the credentials already exist for this access point // check if the credentials already exist for this access point
// note: this is nicer but it's an unstable feature: // note: this is nicer but it's an unstable feature:
@ -634,13 +640,13 @@ pub fn add_credentials(wifi: Form<WiFi>, _auth: Authenticated) -> Template {
let creds_exist = network_client::saved_ap(&wifi.ssid).unwrap_or(false); let creds_exist = network_client::saved_ap(&wifi.ssid).unwrap_or(false);
if creds_exist { if creds_exist {
let mut context = NetworkAddContext::build(); let mut context = NetworkAddContext::build();
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = context.flash_msg =
Some("Network credentials already exist for this access point".to_string()); Some("Network credentials already exist for this access point".to_string());
context.title = Some("Add WiFi Network".to_string()); context.title = Some("Add WiFi Network".to_string());
// return early from handler with "creds already exist" message // return early from handler with "creds already exist" message
return Template::render("network_add", &context); return Template::render("settings/network/network_add", &context);
}; };
// if credentials not found, generate and write wifi config to wpa_supplicant // if credentials not found, generate and write wifi config to wpa_supplicant
@ -653,20 +659,20 @@ pub fn add_credentials(wifi: Form<WiFi>, _auth: Authenticated) -> Template {
Err(_) => warn!("Failed to reconfigure wpa_supplicant"), Err(_) => warn!("Failed to reconfigure wpa_supplicant"),
} }
let mut context = NetworkAddContext::build(); let mut context = NetworkAddContext::build();
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.flash_name = Some("success".to_string()); context.flash_name = Some("success".to_string());
context.flash_msg = Some("Added WiFi credentials".to_string()); context.flash_msg = Some("Added WiFi credentials".to_string());
context.title = Some("Add WiFi Network".to_string()); context.title = Some("Add WiFi Network".to_string());
Template::render("network_add", &context) Template::render("settings/network/network_add", &context)
} }
Err(_) => { Err(_) => {
debug!("Failed to add WiFi credentials."); debug!("Failed to add WiFi credentials.");
let mut context = NetworkAddContext::build(); let mut context = NetworkAddContext::build();
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.flash_name = Some("error".to_string()); context.flash_name = Some("error".to_string());
context.flash_msg = Some("Failed to add WiFi credentials".to_string()); context.flash_msg = Some("Failed to add WiFi credentials".to_string());
context.title = Some("Add WiFi Network".to_string()); context.title = Some("Add WiFi Network".to_string());
Template::render("network_add", &context) Template::render("settings/network/network_add", &context)
} }
} }
} }
@ -719,11 +725,11 @@ impl NetworkAlertContext {
} }
} }
#[get("/network/wifi/usage")] #[get("/wifi/usage")]
pub fn wifi_usage(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn wifi_usage(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = NetworkAlertContext::build(); let mut context = NetworkAlertContext::build();
// set back icon link to network route // set back icon link to network route
context.back = Some("/network".to_string()); context.back = Some("/settings/network".to_string());
context.title = Some("Network Data Usage".to_string()); context.title = Some("Network Data Usage".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
@ -732,30 +738,32 @@ pub fn wifi_usage(flash: Option<FlashMessage>, _auth: Authenticated) -> Template
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
// template_dir is set in Rocket.toml // template_dir is set in Rocket.toml
Template::render("network_usage", &context) Template::render("settings/network/network_usage", &context)
} }
#[post("/network/wifi/usage", data = "<thresholds>")] #[post("/wifi/usage", data = "<thresholds>")]
pub fn wifi_usage_alerts(thresholds: Form<Threshold>, _auth: Authenticated) -> Flash<Redirect> { pub fn wifi_usage_alerts(thresholds: Form<Threshold>, _auth: Authenticated) -> Flash<Redirect> {
match monitor::update_store(thresholds.into_inner()) { match monitor::update_store(thresholds.into_inner()) {
Ok(_) => { Ok(_) => {
debug!("WiFi data usage thresholds updated."); debug!("WiFi data usage thresholds updated.");
Flash::success( Flash::success(
Redirect::to("/network/wifi/usage"), Redirect::to("/settings/network/wifi/usage"),
"Updated alert thresholds and flags", "Updated alert thresholds and flags",
) )
} }
Err(_) => { Err(_) => {
warn!("Failed to update WiFi data usage thresholds."); warn!("Failed to update WiFi data usage thresholds.");
Flash::error( Flash::error(
Redirect::to("/network/wifi/usage"), Redirect::to("/settings/network/wifi/usage"),
"Failed to update alert thresholds and flags", "Failed to update alert thresholds and flags",
) )
} }
} }
} }
#[post("/api/v1/network/wifi/usage", data = "<thresholds>")] // JSON ROUTES FOR NETWORK SETTINGS
#[post("/wifi/usage", data = "<thresholds>")]
pub fn update_wifi_alerts(thresholds: Json<Threshold>, _auth: Authenticated) -> Value { pub fn update_wifi_alerts(thresholds: Json<Threshold>, _auth: Authenticated) -> Value {
match monitor::update_store(thresholds.into_inner()) { match monitor::update_store(thresholds.into_inner()) {
Ok(_) => { Ok(_) => {
@ -773,7 +781,7 @@ pub fn update_wifi_alerts(thresholds: Json<Threshold>, _auth: Authenticated) ->
} }
} }
#[post("/api/v1/network/wifi/usage/reset")] #[post("/wifi/usage/reset")]
pub fn reset_data_total(_auth: Authenticated) -> Value { pub fn reset_data_total(_auth: Authenticated) -> Value {
match monitor::reset_data() { match monitor::reset_data() {
Ok(_) => { Ok(_) => {
@ -805,7 +813,7 @@ pub fn reset_data_total(_auth: Authenticated) -> Value {
// HELPERS AND ROUTES FOR ACCESS POINT ACTIVATION // HELPERS AND ROUTES FOR ACCESS POINT ACTIVATION
#[post("/api/v1/network/activate_ap")] #[post("/activate_ap")]
pub fn activate_ap(_auth: Authenticated) -> Value { pub fn activate_ap(_auth: Authenticated) -> Value {
// activate the wireless access point // activate the wireless access point
debug!("Activating WiFi access point."); debug!("Activating WiFi access point.");
@ -824,7 +832,7 @@ pub fn activate_ap(_auth: Authenticated) -> Value {
// HELPERS AND ROUTES FOR WIFI CLIENT MANAGEMENT // HELPERS AND ROUTES FOR WIFI CLIENT MANAGEMENT
#[post("/api/v1/network/activate_client")] #[post("/activate_client")]
pub fn activate_client(_auth: Authenticated) -> Value { pub fn activate_client(_auth: Authenticated) -> Value {
// activate the wireless client // activate the wireless client
debug!("Activating WiFi client mode."); debug!("Activating WiFi client mode.");
@ -841,8 +849,8 @@ pub fn activate_client(_auth: Authenticated) -> Value {
} }
} }
#[post("/api/v1/network/wifi", data = "<wifi>")] #[post("/wifi", data = "<wifi>")]
pub fn add_wifi(wifi: Json<WiFi>, _auth: Authenticated) -> Value { pub fn add_wifi_credentials(wifi: Json<WiFi>, _auth: Authenticated) -> Value {
// generate and write wifi config to wpa_supplicant // generate and write wifi config to wpa_supplicant
match network_client::add(&wifi.ssid, &wifi.pass) { match network_client::add(&wifi.ssid, &wifi.pass) {
Ok(_) => { Ok(_) => {
@ -867,7 +875,7 @@ pub fn add_wifi(wifi: Json<WiFi>, _auth: Authenticated) -> Value {
} }
} }
#[post("/api/v1/network/wifi/connect", data = "<ssid>")] #[post("/wifi/connect", data = "<ssid>")]
pub fn connect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value { pub fn connect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value {
// retrieve the id for the given network ssid // retrieve the id for the given network ssid
match network_client::id("wlan0", &ssid.ssid) { match network_client::id("wlan0", &ssid.ssid) {
@ -892,7 +900,7 @@ pub fn connect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value {
} }
} }
#[post("/api/v1/network/wifi/disconnect", data = "<ssid>")] #[post("/wifi/disconnect", data = "<ssid>")]
pub fn disconnect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value { pub fn disconnect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value {
// attempt to disable the current network for wlan0 interface // attempt to disable the current network for wlan0 interface
match network_client::disable("wlan0", &ssid.ssid) { match network_client::disable("wlan0", &ssid.ssid) {
@ -909,7 +917,7 @@ pub fn disconnect_ap(ssid: Json<Ssid>, _auth: Authenticated) -> Value {
} }
} }
#[post("/api/v1/network/wifi/forget", data = "<network>")] #[post("/wifi/forget", data = "<network>")]
pub fn forget_ap(network: Json<Ssid>, _auth: Authenticated) -> Value { pub fn forget_ap(network: Json<Ssid>, _auth: Authenticated) -> Value {
let ssid = &network.ssid; let ssid = &network.ssid;
match network_client::forget("wlan0", ssid) { match network_client::forget("wlan0", ssid) {
@ -928,7 +936,7 @@ pub fn forget_ap(network: Json<Ssid>, _auth: Authenticated) -> Value {
} }
} }
#[post("/api/v1/network/wifi/modify", data = "<wifi>")] #[post("/wifi/modify", data = "<wifi>")]
pub fn modify_password(wifi: Json<WiFi>, _auth: Authenticated) -> Value { pub fn modify_password(wifi: Json<WiFi>, _auth: Authenticated) -> Value {
let ssid = &wifi.ssid; let ssid = &wifi.ssid;
let pass = &wifi.pass; let pass = &wifi.pass;
@ -953,7 +961,7 @@ pub fn modify_password(wifi: Json<WiFi>, _auth: Authenticated) -> Value {
// HELPERS AND ROUTES FOR NETWORK STATE QUERIES // HELPERS AND ROUTES FOR NETWORK STATE QUERIES
#[get("/api/v1/network/ip")] #[get("/ip")]
pub fn return_ip(_auth: Authenticated) -> Value { pub fn return_ip(_auth: Authenticated) -> Value {
// retrieve ip for wlan0 or set to x.x.x.x if not found // retrieve ip for wlan0 or set to x.x.x.x if not found
let wlan_ip = match network_client::ip("wlan0") { let wlan_ip = match network_client::ip("wlan0") {
@ -973,7 +981,7 @@ pub fn return_ip(_auth: Authenticated) -> Value {
build_json_response(status, Some(data), None) build_json_response(status, Some(data), None)
} }
#[get("/api/v1/network/rssi")] #[get("/rssi")]
pub fn return_rssi(_auth: Authenticated) -> Value { pub fn return_rssi(_auth: Authenticated) -> Value {
// retrieve rssi for connected network // retrieve rssi for connected network
match network_client::rssi("wlan0") { match network_client::rssi("wlan0") {
@ -990,7 +998,7 @@ pub fn return_rssi(_auth: Authenticated) -> Value {
} }
} }
#[get("/api/v1/network/ssid")] #[get("/ssid")]
pub fn return_ssid(_auth: Authenticated) -> Value { pub fn return_ssid(_auth: Authenticated) -> Value {
// retrieve ssid for connected network // retrieve ssid for connected network
match network_client::ssid("wlan0") { match network_client::ssid("wlan0") {
@ -1007,7 +1015,7 @@ pub fn return_ssid(_auth: Authenticated) -> Value {
} }
} }
#[get("/api/v1/network/state")] #[get("/state")]
pub fn return_state(_auth: Authenticated) -> Value { pub fn return_state(_auth: Authenticated) -> Value {
// retrieve state of wlan0 or set to x.x.x.x if not found // retrieve state of wlan0 or set to x.x.x.x if not found
let wlan_state = match network_client::state("wlan0") { let wlan_state = match network_client::state("wlan0") {
@ -1027,7 +1035,7 @@ pub fn return_state(_auth: Authenticated) -> Value {
build_json_response(status, Some(data), None) build_json_response(status, Some(data), None)
} }
#[get("/api/v1/network/status")] #[get("/status")]
pub fn return_status(_auth: Authenticated) -> Value { pub fn return_status(_auth: Authenticated) -> Value {
// retrieve status info for wlan0 interface // retrieve status info for wlan0 interface
match network_client::status("wlan0") { match network_client::status("wlan0") {
@ -1044,7 +1052,7 @@ pub fn return_status(_auth: Authenticated) -> Value {
} }
} }
#[get("/api/v1/network/wifi")] #[get("/wifi")]
pub fn scan_networks(_auth: Authenticated) -> Value { pub fn scan_networks(_auth: Authenticated) -> Value {
// retrieve scan results for access-points within range of wlan0 // retrieve scan results for access-points within range of wlan0
match network_client::available_networks("wlan0") { match network_client::available_networks("wlan0") {

View File

@ -16,15 +16,15 @@ use peach_lib::config_manager::load_peach_config;
use peach_lib::stats_client::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}; use peach_lib::stats_client::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat};
use peach_lib::{dyndns_client, network_client, oled_client, sbot_client, stats_client}; use peach_lib::{dyndns_client, network_client, oled_client, sbot_client, stats_client};
use crate::utils::build_json_response;
use crate::routes::authentication::Authenticated; use crate::routes::authentication::Authenticated;
use crate::utils::build_json_response;
use rocket::serde::json::Value; use rocket::serde::json::Value;
// HELPERS AND ROUTES FOR /device // HELPERS AND ROUTES FOR /status
/// System statistics data. /// System statistics data.
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct DeviceContext { pub struct StatusContext {
pub back: Option<String>, pub back: Option<String>,
pub cpu_stat_percent: Option<CpuStatPercentages>, pub cpu_stat_percent: Option<CpuStatPercentages>,
pub disk_stats: Vec<DiskUsage>, pub disk_stats: Vec<DiskUsage>,
@ -43,8 +43,8 @@ pub struct DeviceContext {
pub uptime: Option<i32>, pub uptime: Option<i32>,
} }
impl DeviceContext { impl StatusContext {
pub fn build() -> DeviceContext { pub fn build() -> StatusContext {
// convert result to Option<CpuStatPercentages>, discard any error // convert result to Option<CpuStatPercentages>, discard any error
let cpu_stat_percent = stats_client::cpu_stats_percent().ok(); let cpu_stat_percent = stats_client::cpu_stats_percent().ok();
let load_average = stats_client::load_average().ok(); let load_average = stats_client::load_average().ok();
@ -129,7 +129,7 @@ impl DeviceContext {
} }
} }
DeviceContext { StatusContext {
back: None, back: None,
cpu_stat_percent, cpu_stat_percent,
disk_stats, disk_stats,
@ -150,10 +150,10 @@ impl DeviceContext {
} }
} }
#[get("/device")] #[get("/status")]
pub fn device_stats(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn device_status(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// assign context through context_builder call // assign context through context_builder call
let mut context = DeviceContext::build(); let mut context = StatusContext::build();
context.back = Some("/".to_string()); context.back = Some("/".to_string());
context.title = Some("Device Status".to_string()); context.title = Some("Device Status".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
@ -166,7 +166,7 @@ pub fn device_stats(flash: Option<FlashMessage>, _auth: Authenticated) -> Templa
Template::render("device", &context) Template::render("device", &context)
} }
// HELPERS AND ROUTES FOR /device/reboot // HELPERS AND ROUTES FOR /power/reboot
/// Executes a system command to reboot the device immediately. /// Executes a system command to reboot the device immediately.
pub fn reboot() -> io::Result<Output> { pub fn reboot() -> io::Result<Output> {
@ -181,16 +181,16 @@ pub fn reboot() -> io::Result<Output> {
.output() .output()
} }
#[get("/device/reboot")] #[get("/power/reboot")]
pub fn reboot_cmd(_auth: Authenticated) -> Flash<Redirect> { pub fn reboot_cmd(_auth: Authenticated) -> Flash<Redirect> {
match reboot() { match reboot() {
Ok(_) => Flash::success(Redirect::to("/shutdown"), "Rebooting the device"), Ok(_) => Flash::success(Redirect::to("/power"), "Rebooting the device"),
Err(_) => Flash::error(Redirect::to("/shutdown"), "Failed to reboot the device"), Err(_) => Flash::error(Redirect::to("/power"), "Failed to reboot the device"),
} }
} }
/// JSON request handler for device reboot. /// JSON request handler for device reboot.
#[post("/api/v1/device/reboot")] #[post("/api/v1/admin/reboot")]
pub fn reboot_device(_auth: Authenticated) -> Value { pub fn reboot_device(_auth: Authenticated) -> Value {
match reboot() { match reboot() {
Ok(_) => { Ok(_) => {
@ -208,7 +208,7 @@ pub fn reboot_device(_auth: Authenticated) -> Value {
} }
} }
// HELPERS AND ROUTES FOR /device/shutdown // HELPERS AND ROUTES FOR /power/shutdown
/// Executes a system command to shutdown the device immediately. /// Executes a system command to shutdown the device immediately.
pub fn shutdown() -> io::Result<Output> { pub fn shutdown() -> io::Result<Output> {
@ -219,16 +219,16 @@ pub fn shutdown() -> io::Result<Output> {
Command::new("sudo").arg("shutdown").arg("now").output() Command::new("sudo").arg("shutdown").arg("now").output()
} }
#[get("/device/shutdown")] #[get("/power/shutdown")]
pub fn shutdown_cmd(_auth: Authenticated) -> Flash<Redirect> { pub fn shutdown_cmd(_auth: Authenticated) -> Flash<Redirect> {
match shutdown() { match shutdown() {
Ok(_) => Flash::success(Redirect::to("/shutdown"), "Shutting down the device"), Ok(_) => Flash::success(Redirect::to("/power"), "Shutting down the device"),
Err(_) => Flash::error(Redirect::to("/shutdown"), "Failed to shutdown the device"), Err(_) => Flash::error(Redirect::to("/power"), "Failed to shutdown the device"),
} }
} }
// shutdown the device // shutdown the device
#[post("/api/v1/device/shutdown")] #[post("/power/shutdown")]
pub fn shutdown_device(_auth: Authenticated) -> Value { pub fn shutdown_device(_auth: Authenticated) -> Value {
match shutdown() { match shutdown() {
Ok(_) => { Ok(_) => {
@ -246,19 +246,19 @@ pub fn shutdown_device(_auth: Authenticated) -> Value {
} }
} }
// HELPERS AND ROUTES FOR /shutdown // HELPERS AND ROUTES FOR /power
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct ShutdownContext { pub struct PowerContext {
pub back: Option<String>, pub back: Option<String>,
pub flash_name: Option<String>, pub flash_name: Option<String>,
pub flash_msg: Option<String>, pub flash_msg: Option<String>,
pub title: Option<String>, pub title: Option<String>,
} }
impl ShutdownContext { impl PowerContext {
pub fn build() -> ShutdownContext { pub fn build() -> PowerContext {
ShutdownContext { PowerContext {
back: None, back: None,
flash_name: None, flash_name: None,
flash_msg: None, flash_msg: None,
@ -267,16 +267,16 @@ impl ShutdownContext {
} }
} }
#[get("/shutdown")] #[get("/power")]
pub fn shutdown_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template { pub fn power_menu(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
let mut context = ShutdownContext::build(); let mut context = PowerContext::build();
context.back = Some("/".to_string()); context.back = Some("/".to_string());
context.title = Some("Shutdown Device".to_string()); context.title = Some("Power Menu".to_string());
// check to see if there is a flash message to display // check to see if there is a flash message to display
if let Some(flash) = flash { if let Some(flash) = flash {
// add flash message contents to the context object // add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string()); context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string()); context.flash_msg = Some(flash.message().to_string());
}; };
Template::render("shutdown", &context) Template::render("power", &context)
} }

View File

@ -0,0 +1,2 @@
pub mod device;
pub mod ping;

View File

@ -1,19 +1,19 @@
//! Helper routes for pinging services to check that they are active //! Helper routes for pinging services to check that they are active
use log::{debug, warn}; use log::{debug, warn};
use rocket::get; use rocket::get;
use rocket::serde::json::{Value}; use rocket::serde::json::Value;
use peach_lib::network_client; use peach_lib::network_client;
use peach_lib::oled_client; use peach_lib::oled_client;
use peach_lib::stats_client; use peach_lib::stats_client;
use crate::utils::build_json_response;
use crate::routes::authentication::Authenticated; use crate::routes::authentication::Authenticated;
use crate::utils::build_json_response;
/// Status route: useful for checking connectivity from web client. /// Status route: useful for checking connectivity from web client.
#[get("/api/v1/ping")] #[get("/ping")]
pub fn ping_pong(_auth: Authenticated) -> Value { pub fn ping_pong(_auth: Authenticated) -> Value {
//pub fn ping_pong() -> Value { //pub fn ping_pong() -> Value {
// ping pong // ping pong
let status = "success".to_string(); let status = "success".to_string();
let msg = "pong!".to_string(); let msg = "pong!".to_string();
@ -21,7 +21,7 @@ pub fn ping_pong(_auth: Authenticated) -> Value {
} }
/// Status route: check availability of `peach-network` microservice. /// Status route: check availability of `peach-network` microservice.
#[get("/api/v1/ping/network")] #[get("/ping/network")]
pub fn ping_network(_auth: Authenticated) -> Value { pub fn ping_network(_auth: Authenticated) -> Value {
match network_client::ping() { match network_client::ping() {
Ok(_) => { Ok(_) => {
@ -40,7 +40,7 @@ pub fn ping_network(_auth: Authenticated) -> Value {
} }
/// Status route: check availability of `peach-oled` microservice. /// Status route: check availability of `peach-oled` microservice.
#[get("/api/v1/ping/oled")] #[get("/ping/oled")]
pub fn ping_oled(_auth: Authenticated) -> Value { pub fn ping_oled(_auth: Authenticated) -> Value {
match oled_client::ping() { match oled_client::ping() {
Ok(_) => { Ok(_) => {
@ -59,7 +59,7 @@ pub fn ping_oled(_auth: Authenticated) -> Value {
} }
/// Status route: check availability of `peach-stats` microservice. /// Status route: check availability of `peach-stats` microservice.
#[get("/api/v1/ping/stats")] #[get("/ping/stats")]
pub fn ping_stats(_auth: Authenticated) -> Value { pub fn ping_stats(_auth: Authenticated) -> Value {
match stats_client::ping() { match stats_client::ping() {
Ok(_) => { Ok(_) => {