diff --git a/peach-web/src/routes/catchers.rs b/peach-web/src/routes/catchers.rs index a096cac..0c3523e 100644 --- a/peach-web/src/routes/catchers.rs +++ b/peach-web/src/routes/catchers.rs @@ -1,7 +1,7 @@ use log::debug; -use rocket::{catch}; -use rocket_dyn_templates::Template; +use rocket::catch; use rocket::response::Redirect; +use rocket_dyn_templates::Template; use serde::Serialize; // HELPERS AND ROUTES FOR 404 ERROR @@ -34,7 +34,7 @@ pub fn not_found() -> Template { context.flash_name = Some("error".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 @@ -48,7 +48,7 @@ pub fn internal_error() -> Template { context.flash_name = Some("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 @@ -57,4 +57,4 @@ pub fn internal_error() -> Template { pub fn forbidden() -> Redirect { debug!("403 Forbidden"); Redirect::to("/login") -} \ No newline at end of file +} diff --git a/peach-web/src/routes/index.rs b/peach-web/src/routes/index.rs index 1ca4443..aa1113e 100644 --- a/peach-web/src/routes/index.rs +++ b/peach-web/src/routes/index.rs @@ -24,13 +24,13 @@ impl HomeContext { } #[get("/")] -pub fn index(_auth: Authenticated) -> Template { +pub fn home(_auth: Authenticated) -> Template { let context = HomeContext { flash_name: None, flash_msg: None, title: None, }; - Template::render("index", &context) + Template::render("home", &context) } // HELPERS AND ROUTES FOR /help diff --git a/peach-web/src/routes/mod.rs b/peach-web/src/routes/mod.rs index be18d09..830e23b 100644 --- a/peach-web/src/routes/mod.rs +++ b/peach-web/src/routes/mod.rs @@ -1,7 +1,6 @@ pub mod authentication; -pub mod device; pub mod catchers; pub mod index; -pub mod ping; pub mod scuttlebutt; -pub mod settings; \ No newline at end of file +pub mod settings; +pub mod status; diff --git a/peach-web/src/routes/scuttlebutt.rs b/peach-web/src/routes/scuttlebutt.rs index ec8d826..ede8699 100644 --- a/peach-web/src/routes/scuttlebutt.rs +++ b/peach-web/src/routes/scuttlebutt.rs @@ -45,7 +45,7 @@ pub fn private(flash: Option, _auth: Authenticated) -> Template { context.flash_name = Some(flash.kind().to_string()); context.flash_msg = Some(flash.message().to_string()); }; - Template::render("messages", &context) + Template::render("scuttlebutt/messages", &context) } // HELPERS AND ROUTES FOR /peers @@ -81,7 +81,7 @@ pub fn peers(flash: Option, _auth: Authenticated) -> Template { context.flash_name = Some(flash.kind().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 @@ -209,7 +209,7 @@ pub fn profile( context.flash_name = Some(flash.kind().to_string()); context.flash_msg = Some(flash.message().to_string()); }; - Template::render("profile", &context) + Template::render("scuttlebutt/profile", &context) } // HELPERS AND ROUTES FOR /friends @@ -247,7 +247,7 @@ pub fn friends(flash: Option, _auth: Authenticated) -> Template { context.flash_name = Some(flash.kind().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 @@ -285,7 +285,7 @@ pub fn follows(flash: Option, _auth: Authenticated) -> Template { context.flash_name = Some(flash.kind().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 @@ -323,7 +323,7 @@ pub fn followers(flash: Option, _auth: Authenticated) -> Template context.flash_name = Some(flash.kind().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 @@ -361,5 +361,5 @@ pub fn blocks(flash: Option, _auth: Authenticated) -> Template { context.flash_name = Some(flash.kind().to_string()); context.flash_msg = Some(flash.message().to_string()); }; - Template::render("peers_list", &context) + Template::render("scuttlebutt/peers_list", &context) } diff --git a/peach-web/src/routes/settings/admin.rs b/peach-web/src/routes/settings/admin.rs index 4f8980a..5148a17 100644 --- a/peach-web/src/routes/settings/admin.rs +++ b/peach-web/src/routes/settings/admin.rs @@ -1,12 +1,12 @@ +use rocket::serde::{Deserialize, Serialize}; use rocket::{ + form::{Form, FromForm}, get, post, request::FlashMessage, - form::{Form, FromForm}, response::{Flash, Redirect}, uri, }; use rocket_dyn_templates::Template; -use rocket::serde::{Deserialize, Serialize}; use peach_lib::config_manager; use peach_lib::config_manager::load_peach_config; @@ -39,12 +39,12 @@ impl ConfigureAdminContext { } } -/// View and delete currently configured admin. -#[get("/settings/configure_admin")] +/// Administrator settings menu. View and delete currently configured admin. +#[get("/")] pub fn configure_admin(flash: Option, _auth: Authenticated) -> Template { let mut context = ConfigureAdminContext::build(); - // set back icon link to network route - context.back = Some("/network".to_string()); + // set back icon link to settings route + context.back = Some("/settings".to_string()); context.title = Some("Configure Admin".to_string()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -83,14 +83,14 @@ impl AddAdminContext { pub fn save_add_admin_form(admin_form: AddAdminForm) -> Result<(), PeachWebError> { 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(()) } -#[get("/settings/admin/add")] +#[get("/add")] pub fn add_admin(flash: Option, _auth: Authenticated) -> Template { 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()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -102,7 +102,7 @@ pub fn add_admin(flash: Option, _auth: Authenticated) -> Template Template::render("admin/add_admin", &context) } -#[post("/settings/admin/add", data = "")] +#[post("/add", data = "")] pub fn add_admin_post(add_admin_form: Form, _auth: Authenticated) -> Flash { let result = save_add_admin_form(add_admin_form.into_inner()); let url = uri!(configure_admin); @@ -119,8 +119,11 @@ pub struct DeleteAdminForm { pub ssb_id: String, } -#[post("/settings/admin/delete", data = "")] -pub fn delete_admin_post(delete_admin_form: Form, _auth: Authenticated) -> Flash { +#[post("/delete", data = "")] +pub fn delete_admin_post( + delete_admin_form: Form, + _auth: Authenticated, +) -> Flash { let result = config_manager::delete_ssb_admin_id(&delete_admin_form.ssb_id); let url = uri!(configure_admin); match result { diff --git a/peach-web/src/routes/settings/dns.rs b/peach-web/src/routes/settings/dns.rs index 7bca89c..8ef3e21 100644 --- a/peach-web/src/routes/settings/dns.rs +++ b/peach-web/src/routes/settings/dns.rs @@ -1,12 +1,14 @@ use log::info; use rocket::{ + form::{Form, FromForm}, get, post, request::FlashMessage, - form::{Form, FromForm} + serde::{ + json::{Json, Value}, + Deserialize, Serialize, + }, }; -use rocket::serde::json::Json; use rocket_dyn_templates::Template; -use rocket::serde::{Deserialize, Serialize}; use peach_lib::config_manager; 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::routes::authentication::Authenticated; use crate::utils::build_json_response; -use rocket::serde::json::Value; #[derive(Debug, Deserialize, FromForm)] pub struct DnsForm { @@ -113,11 +114,11 @@ impl ConfigureDNSContext { } } -#[get("/network/dns")] +#[get("/dns")] pub fn configure_dns(flash: Option, _auth: Authenticated) -> Template { let mut context = ConfigureDNSContext::build(); // 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()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -125,35 +126,35 @@ pub fn configure_dns(flash: Option, _auth: Authenticated) -> Templ context.flash_name = Some(flash.kind().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 = "")] +#[post("/dns", data = "")] pub fn configure_dns_post(dns: Form, _auth: Authenticated) -> Template { let result = save_dns_configuration(dns.into_inner()); match result { Ok(_) => { let mut context = ConfigureDNSContext::build(); // 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.flash_name = Some("success".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) => { let mut context = ConfigureDNSContext::build(); // 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.flash_name = Some("error".to_string()); 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 = "")] +#[post("/dns/configure", data = "")] pub fn save_dns_configuration_endpoint(dns_form: Json, _auth: Authenticated) -> Value { let result = save_dns_configuration(dns_form.into_inner()); match result { diff --git a/peach-web/src/routes/settings/menu.rs b/peach-web/src/routes/settings/menu.rs new file mode 100644 index 0000000..e5abe1c --- /dev/null +++ b/peach-web/src/routes/settings/menu.rs @@ -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, + pub title: Option, + pub flash_name: Option, + pub flash_msg: Option, +} + +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, _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) +} diff --git a/peach-web/src/routes/settings/mod.rs b/peach-web/src/routes/settings/mod.rs index 8fee4d7..530c15b 100644 --- a/peach-web/src/routes/settings/mod.rs +++ b/peach-web/src/routes/settings/mod.rs @@ -1,3 +1,4 @@ pub mod admin; pub mod dns; -pub mod network; \ No newline at end of file +pub mod menu; +pub mod network; diff --git a/peach-web/src/routes/settings/network.rs b/peach-web/src/routes/settings/network.rs index 9b80a37..61ae7be 100644 --- a/peach-web/src/routes/settings/network.rs +++ b/peach-web/src/routes/settings/network.rs @@ -1,27 +1,27 @@ use log::{debug, warn}; use rocket::{ - get, - post, - request::FlashMessage, form::{Form, FromForm}, + get, post, + request::FlashMessage, response::{Flash, Redirect}, + serde::{ + json::{json, Json, Value}, + Deserialize, Serialize, + }, uri, UriDisplayQuery, }; -use rocket::serde::json::{json, Json}; use rocket_dyn_templates::Template; -use rocket::serde::{Deserialize, Serialize}; use std::collections::HashMap; use peach_lib::network_client; use peach_lib::network_client::{AccessPoint, Networks, Scan}; 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::{Alert, Data, Threshold}; -use crate::utils::build_json_response; -use crate::routes::authentication::Authenticated; -use rocket::serde::json::Value; // STRUCTS USED BY NETWORK ROUTES @@ -36,9 +36,9 @@ pub struct WiFi { 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 { let url = uri!(wifi_usage); match monitor::reset_data() { @@ -50,7 +50,7 @@ pub fn wifi_usage_reset(_auth: Authenticated) -> Flash { } } -#[post("/network/wifi/connect", data = "")] +#[post("/wifi/connect", data = "")] pub fn connect_wifi(network: Form, _auth: Authenticated) -> Flash { let ssid = &network.ssid; let url = uri!(network_detail(ssid = ssid)); @@ -63,7 +63,7 @@ pub fn connect_wifi(network: Form, _auth: Authenticated) -> Flash, _auth: Authenticated) -> Flash { let ssid = &network.ssid; let url = uri!(network_home); @@ -73,7 +73,7 @@ pub fn disconnect_wifi(network: Form, _auth: Authenticated) -> Flash, _auth: Authenticated) -> Flash { let ssid = &network.ssid; let url = uri!(network_home); @@ -86,10 +86,10 @@ pub fn forget_wifi(network: Form, _auth: Authenticated) -> Flash } } -#[get("/network/wifi/modify?")] +#[get("/wifi/modify?")] pub fn wifi_password(ssid: &str, flash: Option, _auth: Authenticated) -> Template { let mut context = NetworkAddContext { - back: Some("/network/wifi".to_string()), + back: Some("/settings/network/wifi".to_string()), flash_name: None, flash_msg: None, selected: Some(ssid.to_string()), @@ -102,10 +102,10 @@ pub fn wifi_password(ssid: &str, flash: Option, _auth: Authenticat context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("network_modify", &context) + Template::render("settings/network/network_modify", &context) } -#[post("/network/wifi/modify", data = "")] +#[post("/wifi/modify", data = "")] pub fn wifi_set_password(wifi: Form, _auth: Authenticated) -> Flash { let ssid = &wifi.ssid; let pass = &wifi.pass; @@ -119,7 +119,7 @@ pub fn wifi_set_password(wifi: Form, _auth: Authenticated) -> Flash, _auth: Authenticated) -> Template { // assign context through context_builder call let mut context = NetworkContext::build(); // set back button (nav) url - context.back = Some("/".to_string()); + context.back = Some("/settings".to_string()); // set page title context.title = Some("Network Configuration".to_string()); // check to see if there is a flash message to display @@ -288,25 +288,28 @@ pub fn network_home(flash: Option, _auth: Authenticated) -> Templa context.flash_msg = Some(flash.message().to_string()); }; // 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 { // activate the wireless access point debug!("Activating WiFi access point."); 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( - Redirect::to("/network"), + Redirect::to("/settings/network"), "Failed to activate WiFi access point", ), } } -// HELPERS AND ROUTES FOR /network/wifi +// HELPERS AND ROUTES FOR /settings/network/wifi #[derive(Debug, Serialize)] pub struct NetworkListContext { @@ -375,11 +378,11 @@ impl NetworkListContext { } } -#[get("/network/wifi")] +#[get("/wifi")] pub fn wifi_list(flash: Option, _auth: Authenticated) -> Template { // assign context through context_builder call 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()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -388,10 +391,10 @@ pub fn wifi_list(flash: Option, _auth: Authenticated) -> Template context.flash_msg = Some(flash.message().to_string()); }; // 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 +// HELPERS AND ROUTES FOR /settings/network/wifi #[derive(Debug, Serialize)] pub struct NetworkDetailContext { @@ -540,11 +543,11 @@ impl NetworkDetailContext { } } -#[get("/network/wifi?")] +#[get("/wifi?")] pub fn network_detail(ssid: &str, flash: Option, _auth: Authenticated) -> Template { // assign context through context_builder call 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.selected = Some(ssid.to_string()); // check to see if there is a flash message to display @@ -554,28 +557,31 @@ pub fn network_detail(ssid: &str, flash: Option, _auth: Authentica context.flash_msg = Some(flash.message().to_string()); }; // 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 { // activate the wireless client debug!("Activating WiFi client mode."); match network_client::activate_client() { - Ok(_) => Flash::success(Redirect::to("/network"), "Activated WiFi client"), - Err(_) => Flash::error(Redirect::to("/network"), "Failed to activate WiFi client"), + Ok(_) => Flash::success(Redirect::to("/settings/network"), "Activated 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")] -pub fn network_add_wifi(flash: Option, _auth: Authenticated) -> Template { +#[get("/wifi/add")] +pub fn add_wifi(flash: Option, _auth: Authenticated) -> Template { let mut context = NetworkContext::build(); // 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()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -584,10 +590,10 @@ pub fn network_add_wifi(flash: Option, _auth: Authenticated) -> Te context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("network_add", &context) + Template::render("settings/network/network_add", &context) } -// used in /network/wifi/add? +// used in /settings/network/wifi/add? #[derive(Debug, Serialize)] pub struct NetworkAddContext { pub back: Option, @@ -609,10 +615,10 @@ impl NetworkAddContext { } } -#[get("/network/wifi/add?")] -pub fn network_add_ssid(ssid: &str, flash: Option, _auth: Authenticated) -> Template { +#[get("/wifi/add?")] +pub fn add_ssid(ssid: &str, flash: Option, _auth: Authenticated) -> Template { 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.title = Some("Add WiFi Network".to_string()); // check to see if there is a flash message to display @@ -622,10 +628,10 @@ pub fn network_add_ssid(ssid: &str, flash: Option, _auth: Authenti context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("network_add", &context) + Template::render("settings/network/network_add", &context) } -#[post("/network/wifi/add", data = "")] +#[post("/wifi/add", data = "")] pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { // check if the credentials already exist for this access point // note: this is nicer but it's an unstable feature: @@ -634,13 +640,13 @@ pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { let creds_exist = network_client::saved_ap(&wifi.ssid).unwrap_or(false); if creds_exist { 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_msg = Some("Network credentials already exist for this access point".to_string()); context.title = Some("Add WiFi Network".to_string()); // 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 @@ -653,20 +659,20 @@ pub fn add_credentials(wifi: Form, _auth: Authenticated) -> Template { Err(_) => warn!("Failed to reconfigure wpa_supplicant"), } 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_msg = Some("Added WiFi credentials".to_string()); context.title = Some("Add WiFi Network".to_string()); - Template::render("network_add", &context) + Template::render("settings/network/network_add", &context) } Err(_) => { debug!("Failed to add WiFi credentials."); 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_msg = Some("Failed to add WiFi credentials".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, _auth: Authenticated) -> Template { let mut context = NetworkAlertContext::build(); // 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()); // check to see if there is a flash message to display if let Some(flash) = flash { @@ -732,30 +738,32 @@ pub fn wifi_usage(flash: Option, _auth: Authenticated) -> Template context.flash_msg = Some(flash.message().to_string()); }; // template_dir is set in Rocket.toml - Template::render("network_usage", &context) + Template::render("settings/network/network_usage", &context) } -#[post("/network/wifi/usage", data = "")] +#[post("/wifi/usage", data = "")] pub fn wifi_usage_alerts(thresholds: Form, _auth: Authenticated) -> Flash { match monitor::update_store(thresholds.into_inner()) { Ok(_) => { debug!("WiFi data usage thresholds updated."); Flash::success( - Redirect::to("/network/wifi/usage"), + Redirect::to("/settings/network/wifi/usage"), "Updated alert thresholds and flags", ) } Err(_) => { warn!("Failed to update WiFi data usage thresholds."); Flash::error( - Redirect::to("/network/wifi/usage"), + Redirect::to("/settings/network/wifi/usage"), "Failed to update alert thresholds and flags", ) } } } -#[post("/api/v1/network/wifi/usage", data = "")] +// JSON ROUTES FOR NETWORK SETTINGS + +#[post("/wifi/usage", data = "")] pub fn update_wifi_alerts(thresholds: Json, _auth: Authenticated) -> Value { match monitor::update_store(thresholds.into_inner()) { Ok(_) => { @@ -773,7 +781,7 @@ pub fn update_wifi_alerts(thresholds: Json, _auth: Authenticated) -> } } -#[post("/api/v1/network/wifi/usage/reset")] +#[post("/wifi/usage/reset")] pub fn reset_data_total(_auth: Authenticated) -> Value { match monitor::reset_data() { Ok(_) => { @@ -805,7 +813,7 @@ pub fn reset_data_total(_auth: Authenticated) -> Value { // HELPERS AND ROUTES FOR ACCESS POINT ACTIVATION -#[post("/api/v1/network/activate_ap")] +#[post("/activate_ap")] pub fn activate_ap(_auth: Authenticated) -> Value { // activate the wireless 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 -#[post("/api/v1/network/activate_client")] +#[post("/activate_client")] pub fn activate_client(_auth: Authenticated) -> Value { // activate the wireless client debug!("Activating WiFi client mode."); @@ -841,8 +849,8 @@ pub fn activate_client(_auth: Authenticated) -> Value { } } -#[post("/api/v1/network/wifi", data = "")] -pub fn add_wifi(wifi: Json, _auth: Authenticated) -> Value { +#[post("/wifi", data = "")] +pub fn add_wifi_credentials(wifi: Json, _auth: Authenticated) -> Value { // generate and write wifi config to wpa_supplicant match network_client::add(&wifi.ssid, &wifi.pass) { Ok(_) => { @@ -867,7 +875,7 @@ pub fn add_wifi(wifi: Json, _auth: Authenticated) -> Value { } } -#[post("/api/v1/network/wifi/connect", data = "")] +#[post("/wifi/connect", data = "")] pub fn connect_ap(ssid: Json, _auth: Authenticated) -> Value { // retrieve the id for the given network ssid match network_client::id("wlan0", &ssid.ssid) { @@ -892,7 +900,7 @@ pub fn connect_ap(ssid: Json, _auth: Authenticated) -> Value { } } -#[post("/api/v1/network/wifi/disconnect", data = "")] +#[post("/wifi/disconnect", data = "")] pub fn disconnect_ap(ssid: Json, _auth: Authenticated) -> Value { // attempt to disable the current network for wlan0 interface match network_client::disable("wlan0", &ssid.ssid) { @@ -909,7 +917,7 @@ pub fn disconnect_ap(ssid: Json, _auth: Authenticated) -> Value { } } -#[post("/api/v1/network/wifi/forget", data = "")] +#[post("/wifi/forget", data = "")] pub fn forget_ap(network: Json, _auth: Authenticated) -> Value { let ssid = &network.ssid; match network_client::forget("wlan0", ssid) { @@ -928,7 +936,7 @@ pub fn forget_ap(network: Json, _auth: Authenticated) -> Value { } } -#[post("/api/v1/network/wifi/modify", data = "")] +#[post("/wifi/modify", data = "")] pub fn modify_password(wifi: Json, _auth: Authenticated) -> Value { let ssid = &wifi.ssid; let pass = &wifi.pass; @@ -953,7 +961,7 @@ pub fn modify_password(wifi: Json, _auth: Authenticated) -> Value { // HELPERS AND ROUTES FOR NETWORK STATE QUERIES -#[get("/api/v1/network/ip")] +#[get("/ip")] pub fn return_ip(_auth: Authenticated) -> Value { // retrieve ip for wlan0 or set to x.x.x.x if not found 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) } -#[get("/api/v1/network/rssi")] +#[get("/rssi")] pub fn return_rssi(_auth: Authenticated) -> Value { // retrieve rssi for connected network 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 { // retrieve ssid for connected network 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 { // retrieve state of wlan0 or set to x.x.x.x if not found 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) } -#[get("/api/v1/network/status")] +#[get("/status")] pub fn return_status(_auth: Authenticated) -> Value { // retrieve status info for wlan0 interface 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 { // retrieve scan results for access-points within range of wlan0 match network_client::available_networks("wlan0") { diff --git a/peach-web/src/routes/device.rs b/peach-web/src/routes/status/device.rs similarity index 86% rename from peach-web/src/routes/device.rs rename to peach-web/src/routes/status/device.rs index d06983b..f67c9aa 100644 --- a/peach-web/src/routes/device.rs +++ b/peach-web/src/routes/status/device.rs @@ -16,15 +16,15 @@ use peach_lib::config_manager::load_peach_config; use peach_lib::stats_client::{CpuStatPercentages, DiskUsage, LoadAverage, MemStat}; 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::utils::build_json_response; use rocket::serde::json::Value; -// HELPERS AND ROUTES FOR /device +// HELPERS AND ROUTES FOR /status /// System statistics data. #[derive(Debug, Serialize)] -pub struct DeviceContext { +pub struct StatusContext { pub back: Option, pub cpu_stat_percent: Option, pub disk_stats: Vec, @@ -43,8 +43,8 @@ pub struct DeviceContext { pub uptime: Option, } -impl DeviceContext { - pub fn build() -> DeviceContext { +impl StatusContext { + pub fn build() -> StatusContext { // convert result to Option, discard any error let cpu_stat_percent = stats_client::cpu_stats_percent().ok(); let load_average = stats_client::load_average().ok(); @@ -129,7 +129,7 @@ impl DeviceContext { } } - DeviceContext { + StatusContext { back: None, cpu_stat_percent, disk_stats, @@ -150,10 +150,10 @@ impl DeviceContext { } } -#[get("/device")] -pub fn device_stats(flash: Option, _auth: Authenticated) -> Template { +#[get("/status")] +pub fn device_status(flash: Option, _auth: Authenticated) -> Template { // assign context through context_builder call - let mut context = DeviceContext::build(); + let mut context = StatusContext::build(); context.back = Some("/".to_string()); context.title = Some("Device Status".to_string()); // check to see if there is a flash message to display @@ -166,7 +166,7 @@ pub fn device_stats(flash: Option, _auth: Authenticated) -> Templa 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. pub fn reboot() -> io::Result { @@ -181,16 +181,16 @@ pub fn reboot() -> io::Result { .output() } -#[get("/device/reboot")] +#[get("/power/reboot")] pub fn reboot_cmd(_auth: Authenticated) -> Flash { match reboot() { - Ok(_) => Flash::success(Redirect::to("/shutdown"), "Rebooting the device"), - Err(_) => Flash::error(Redirect::to("/shutdown"), "Failed to reboot the device"), + Ok(_) => Flash::success(Redirect::to("/power"), "Rebooting the device"), + Err(_) => Flash::error(Redirect::to("/power"), "Failed to reboot the device"), } } /// JSON request handler for device reboot. -#[post("/api/v1/device/reboot")] +#[post("/api/v1/admin/reboot")] pub fn reboot_device(_auth: Authenticated) -> Value { match reboot() { 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. pub fn shutdown() -> io::Result { @@ -219,16 +219,16 @@ pub fn shutdown() -> io::Result { Command::new("sudo").arg("shutdown").arg("now").output() } -#[get("/device/shutdown")] +#[get("/power/shutdown")] pub fn shutdown_cmd(_auth: Authenticated) -> Flash { match shutdown() { - Ok(_) => Flash::success(Redirect::to("/shutdown"), "Shutting down the device"), - Err(_) => Flash::error(Redirect::to("/shutdown"), "Failed to shutdown the device"), + Ok(_) => Flash::success(Redirect::to("/power"), "Shutting down the device"), + Err(_) => Flash::error(Redirect::to("/power"), "Failed to shutdown the device"), } } // shutdown the device -#[post("/api/v1/device/shutdown")] +#[post("/power/shutdown")] pub fn shutdown_device(_auth: Authenticated) -> Value { match shutdown() { 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)] -pub struct ShutdownContext { +pub struct PowerContext { pub back: Option, pub flash_name: Option, pub flash_msg: Option, pub title: Option, } -impl ShutdownContext { - pub fn build() -> ShutdownContext { - ShutdownContext { +impl PowerContext { + pub fn build() -> PowerContext { + PowerContext { back: None, flash_name: None, flash_msg: None, @@ -267,16 +267,16 @@ impl ShutdownContext { } } -#[get("/shutdown")] -pub fn shutdown_menu(flash: Option, _auth: Authenticated) -> Template { - let mut context = ShutdownContext::build(); +#[get("/power")] +pub fn power_menu(flash: Option, _auth: Authenticated) -> Template { + let mut context = PowerContext::build(); 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 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("shutdown", &context) + Template::render("power", &context) } diff --git a/peach-web/src/routes/status/mod.rs b/peach-web/src/routes/status/mod.rs new file mode 100644 index 0000000..cd4d59c --- /dev/null +++ b/peach-web/src/routes/status/mod.rs @@ -0,0 +1,2 @@ +pub mod device; +pub mod ping; diff --git a/peach-web/src/routes/ping.rs b/peach-web/src/routes/status/ping.rs similarity index 93% rename from peach-web/src/routes/ping.rs rename to peach-web/src/routes/status/ping.rs index aed39ec..3f83002 100644 --- a/peach-web/src/routes/ping.rs +++ b/peach-web/src/routes/status/ping.rs @@ -1,19 +1,19 @@ //! Helper routes for pinging services to check that they are active use log::{debug, warn}; use rocket::get; -use rocket::serde::json::{Value}; +use rocket::serde::json::Value; use peach_lib::network_client; use peach_lib::oled_client; use peach_lib::stats_client; -use crate::utils::build_json_response; use crate::routes::authentication::Authenticated; +use crate::utils::build_json_response; /// 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() -> Value { + //pub fn ping_pong() -> Value { // ping pong let status = "success".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. -#[get("/api/v1/ping/network")] +#[get("/ping/network")] pub fn ping_network(_auth: Authenticated) -> Value { match network_client::ping() { Ok(_) => { @@ -40,7 +40,7 @@ pub fn ping_network(_auth: Authenticated) -> Value { } /// Status route: check availability of `peach-oled` microservice. -#[get("/api/v1/ping/oled")] +#[get("/ping/oled")] pub fn ping_oled(_auth: Authenticated) -> Value { match oled_client::ping() { Ok(_) => { @@ -59,7 +59,7 @@ pub fn ping_oled(_auth: Authenticated) -> Value { } /// Status route: check availability of `peach-stats` microservice. -#[get("/api/v1/ping/stats")] +#[get("/ping/stats")] pub fn ping_stats(_auth: Authenticated) -> Value { match stats_client::ping() { Ok(_) => {