From b0b21ad8a012d5f5fd37949a056ac7af59ed0897 Mon Sep 17 00:00:00 2001 From: glyph Date: Thu, 13 Jan 2022 13:15:42 +0200 Subject: [PATCH] add standalone check before mounting routes --- peach-web/Cargo.toml | 1 + peach-web/src/main.rs | 113 ++++++-------------------------- peach-web/src/router.rs | 142 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 92 deletions(-) create mode 100644 peach-web/src/router.rs diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index fbaced9..409c82c 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -37,6 +37,7 @@ maintenance = { status = "actively-developed" } [dependencies] env_logger = "0.8" +lazy_static = "1.4.0" log = "0.4" nest = "1.0.0" peach-lib = { path = "../peach-lib" } diff --git a/peach-web/src/main.rs b/peach-web/src/main.rs index 481f7a6..92528d6 100644 --- a/peach-web/src/main.rs +++ b/peach-web/src/main.rs @@ -25,106 +25,36 @@ #![feature(proc_macro_hygiene, decl_macro)] pub mod error; +mod router; pub mod routes; #[cfg(test)] mod tests; pub mod utils; +use std::{env, process}; + +use lazy_static::lazy_static; use log::{error, info}; -use std::process; - -use rocket::{catchers, fs::FileServer, routes, Build, Rocket}; -use rocket_dyn_templates::Template; - -use crate::routes::authentication::*; -use crate::routes::catchers::*; -use crate::routes::index::*; -use crate::routes::scuttlebutt::*; -use crate::routes::status::device::*; -use crate::routes::status::network::*; - -use crate::routes::settings::admin::*; -use crate::routes::settings::dns::*; -use crate::routes::settings::menu::*; -use crate::routes::settings::network::*; -use crate::routes::settings::scuttlebutt::*; +use rocket::{Build, Rocket}; pub type BoxError = Box; -/// Create rocket instance & mount all routes. -fn init_rocket() -> Rocket { - rocket::build() - // GENERAL HTML ROUTES - .mount( - "/", - routes![ - help, - home, - login, - login_post, - logout, - reboot_cmd, - shutdown_cmd, - power_menu, - settings_menu, - ], - ) - // STATUS HTML ROUTES - .mount("/status", routes![device_status, network_status]) - // ADMIN SETTINGS HTML ROUTES - .mount( - "/settings/admin", - routes![ - admin_menu, - configure_admin, - add_admin, - add_admin_post, - delete_admin_post, - change_password, - change_password_post, - reset_password, - reset_password_post, - forgot_password_page, - send_password_reset_post, - ], - ) - // NETWORK SETTINGS HTML ROUTES - .mount( - "/settings/network", - routes![ - add_credentials, - connect_wifi, - configure_dns, - configure_dns_post, - disconnect_wifi, - deploy_ap, - deploy_client, - forget_wifi, - network_home, - add_ssid, - add_wifi, - network_detail, - wifi_list, - wifi_password, - wifi_set_password, - wifi_usage, - wifi_usage_alerts, - wifi_usage_reset, - ], - ) - // SCUTTLEBUTT SETTINGS HTML ROUTES - .mount("/settings/scuttlebutt", routes![ssb_settings_menu]) - // SCUTTLEBUTT SOCIAL HTML ROUTES - .mount( - "/scuttlebutt", - routes![ - peers, friends, follows, followers, blocks, profile, private, follow, unfollow, - block, publish, - ], - ) - .mount("/", FileServer::from("static")) - .register("/", catchers![not_found, internal_error, forbidden]) - .attach(Template::fairing()) +lazy_static! { + // determine run-mode from env var; default to standalone mode (aka peachpub) + static ref STANDALONE_MODE: bool = match env::var("PEACH_STANDALONE_MODE") { + // parse the value to a boolean; default to true for any error + Ok(val) => val.parse().unwrap_or(true), + Err(_) => true + }; +} + +pub fn init_rocket() -> Rocket { + info!("Initializing Rocket"); + if *STANDALONE_MODE { + router::build_minimal_rocket() + } else { + router::build_complete_rocket() + } } /// Launch the peach-web rocket server. @@ -134,7 +64,6 @@ async fn main() { env_logger::init(); // initialize rocket - info!("Initializing Rocket"); let rocket = init_rocket(); // launch rocket diff --git a/peach-web/src/router.rs b/peach-web/src/router.rs new file mode 100644 index 0000000..f86148b --- /dev/null +++ b/peach-web/src/router.rs @@ -0,0 +1,142 @@ +use rocket::{catchers, fs::FileServer, routes, Build, Rocket}; +use rocket_dyn_templates::Template; + +use crate::routes::{ + authentication::*, + catchers::*, + index::*, + scuttlebutt::*, + settings::{admin::*, dns::*, menu::*, network::*, scuttlebutt::*}, + status::{device::*, network::*}, +}; + +/// Create minimal rocket instance and mount routes. This excludes settings +/// and status routes related to networking and the device (memory, +/// hard disk, CPU etc.). +pub fn build_minimal_rocket() -> Rocket { + rocket::build() + // GENERAL HTML ROUTES + .mount( + "/", + routes![ + help, + home, + login, + login_post, + logout, + reboot_cmd, + shutdown_cmd, + power_menu, + settings_menu, + ], + ) + // ADMIN SETTINGS HTML ROUTES + .mount( + "/settings/admin", + routes![ + admin_menu, + configure_admin, + add_admin, + add_admin_post, + delete_admin_post, + change_password, + change_password_post, + reset_password, + reset_password_post, + forgot_password_page, + send_password_reset_post, + ], + ) + // SCUTTLEBUTT SETTINGS HTML ROUTES + .mount("/settings/scuttlebutt", routes![ssb_settings_menu]) + // SCUTTLEBUTT SOCIAL HTML ROUTES + .mount( + "/scuttlebutt", + routes![ + peers, friends, follows, followers, blocks, profile, private, follow, unfollow, + block, publish, + ], + ) + // STATUS HTML ROUTES + // TODO: replace this with a route for `scuttlebutt_status` + .mount("/status", routes![device_status, network_status]) + .mount("/", FileServer::from("static")) + .register("/", catchers![not_found, internal_error, forbidden]) + .attach(Template::fairing()) +} + +/// Create complete rocket instance and mount all routes. +pub fn build_complete_rocket() -> Rocket { + rocket::build() + // GENERAL HTML ROUTES + .mount( + "/", + routes![ + help, + home, + login, + login_post, + logout, + reboot_cmd, + shutdown_cmd, + power_menu, + settings_menu, + ], + ) + // ADMIN SETTINGS HTML ROUTES + .mount( + "/settings/admin", + routes![ + admin_menu, + configure_admin, + add_admin, + add_admin_post, + delete_admin_post, + change_password, + change_password_post, + reset_password, + reset_password_post, + forgot_password_page, + send_password_reset_post, + ], + ) + // NETWORK SETTINGS HTML ROUTES + .mount( + "/settings/network", + routes![ + add_credentials, + connect_wifi, + configure_dns, + configure_dns_post, + disconnect_wifi, + deploy_ap, + deploy_client, + forget_wifi, + network_home, + add_ssid, + add_wifi, + network_detail, + wifi_list, + wifi_password, + wifi_set_password, + wifi_usage, + wifi_usage_alerts, + wifi_usage_reset, + ], + ) + // SCUTTLEBUTT SETTINGS HTML ROUTES + .mount("/settings/scuttlebutt", routes![ssb_settings_menu]) + // SCUTTLEBUTT SOCIAL HTML ROUTES + .mount( + "/scuttlebutt", + routes![ + peers, friends, follows, followers, blocks, profile, private, follow, unfollow, + block, publish, + ], + ) + // STATUS HTML ROUTES + .mount("/status", routes![device_status, network_status]) + .mount("/", FileServer::from("static")) + .register("/", catchers![not_found, internal_error, forbidden]) + .attach(Template::fairing()) +}