211 lines
6.8 KiB
Rust
211 lines
6.8 KiB
Rust
//! # peach-web
|
|
//!
|
|
//! `peach-web` provides a web interface for monitoring and interacting with the
|
|
//! PeachCloud device. This allows administration of the single-board computer
|
|
//! (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related
|
|
//! plugins.
|
|
//!
|
|
//! ## Design
|
|
//!
|
|
//! `peach-web` is written primarily in Rust and presents a web interface for
|
|
//! interacting with the device. The stack currently consists of Rouille (Rust
|
|
//! micro-web-framework), Maud (an HTML template engine for Rust), HTML and
|
|
//! CSS.
|
|
|
|
//mod context;
|
|
mod config;
|
|
pub mod error;
|
|
//mod router;
|
|
mod routes;
|
|
//#[cfg(test)]
|
|
//mod tests;
|
|
mod templates;
|
|
pub mod utils;
|
|
|
|
use std::sync::RwLock;
|
|
|
|
use lazy_static::lazy_static;
|
|
//use log::{debug, error, info};
|
|
use log::info;
|
|
//use peach_lib::{config_manager, config_manager::YAML_PATH as PEACH_CONFIG};
|
|
//use rocket::{fairing::AdHoc, serde::Deserialize, Build, Rocket};
|
|
|
|
use rouille::{router, Response};
|
|
|
|
// crate-local dependencies
|
|
use config::Config;
|
|
use utils::{flash::FlashResponse, theme::Theme};
|
|
|
|
pub type BoxError = Box<dyn std::error::Error>;
|
|
|
|
/*
|
|
/// Application configuration parameters.
|
|
/// These values are extracted from Rocket's default configuration provider:
|
|
/// `Config::figment()`. As such, the values are drawn from `Rocket.toml` or
|
|
/// the TOML file path in the `ROCKET_CONFIG` environment variable. The TOML
|
|
/// file parameters are automatically overruled by any `ROCKET_` variables
|
|
/// which might be set.
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct RocketConfig {
|
|
disable_auth: bool,
|
|
standalone_mode: bool,
|
|
}
|
|
*/
|
|
|
|
static CONFIG_PATH: &str = "./config";
|
|
|
|
lazy_static! {
|
|
static ref CONFIG: Config = Config::from_file(CONFIG_PATH).expect("failed to read config file");
|
|
static ref THEME: RwLock<Theme> = RwLock::new(Theme::Light);
|
|
}
|
|
|
|
//static WLAN_IFACE: &str = "wlan0";
|
|
//static AP_IFACE: &str = "ap0";
|
|
|
|
/*
|
|
pub fn init_rocket() -> Rocket<Build> {
|
|
info!("Initializing Rocket");
|
|
// build a basic rocket instance
|
|
let rocket = rocket::build();
|
|
|
|
// return the default provider figment used by `rocket::build()`
|
|
let figment = rocket.figment();
|
|
|
|
// deserialize configuration parameters into our `RocketConfig` struct (defined above)
|
|
// since we're in the intialisation phase, panic if the extraction fails
|
|
let config: RocketConfig = figment.extract().expect("configuration extraction failed");
|
|
|
|
debug!("{:?}", config);
|
|
|
|
info!("Mounting Rocket routes");
|
|
let mounted_rocket = if config.standalone_mode {
|
|
router::mount_peachpub_routes(rocket)
|
|
} else {
|
|
router::mount_peachcloud_routes(rocket)
|
|
};
|
|
|
|
info!("Attaching application configuration to managed state");
|
|
mounted_rocket.attach(AdHoc::config::<RocketConfig>())
|
|
}
|
|
*/
|
|
|
|
const HOSTNAME_AND_PORT: &str = "localhost:8000";
|
|
|
|
/// Launch the peach-web server.
|
|
fn main() {
|
|
// initialize logger
|
|
env_logger::init();
|
|
|
|
/*
|
|
// check if /var/lib/peachcloud/config.yml exists
|
|
if !std::path::Path::new(PEACH_CONFIG).exists() {
|
|
info!("PeachCloud configuration file not found; loading default values");
|
|
// since we're in the intialisation phase, panic if the loading fails
|
|
let config =
|
|
config_manager::load_peach_config().expect("peachcloud configuration loading failed");
|
|
|
|
info!("Saving default PeachCloud configuration values to file");
|
|
// this ensures a config file is created if it does not already exist
|
|
config_manager::save_peach_config(config).expect("peachcloud configuration saving failed");
|
|
}
|
|
*/
|
|
|
|
info!("Launching web server...");
|
|
|
|
// the `start_server` starts listening forever on the given address.
|
|
rouille::start_server(HOSTNAME_AND_PORT, move |request| {
|
|
info!("Now listening on {}", HOSTNAME_AND_PORT);
|
|
|
|
// static file server
|
|
// matches on assets in the `static` directory
|
|
let response = rouille::match_assets(request, "static");
|
|
if response.is_success() {
|
|
return response;
|
|
}
|
|
|
|
router!(request,
|
|
(GET) (/) => {
|
|
Response::html(routes::home::build_template())
|
|
},
|
|
|
|
(GET) (/auth/change) => {
|
|
// build the html template
|
|
Response::html(routes::authentication::change::build_template(request))
|
|
// reset the flash msg cookies in the response object
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/auth/change) => {
|
|
routes::authentication::change::handle_form(request)
|
|
},
|
|
|
|
(GET) (/auth/login) => {
|
|
Response::html(routes::authentication::login::build_template(request))
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/auth/login) => {
|
|
routes::authentication::login::handle_form(request)
|
|
},
|
|
|
|
(GET) (/auth/logout) => {
|
|
routes::authentication::logout::deauthenticate()
|
|
},
|
|
|
|
(GET) (/auth/reset) => {
|
|
Response::html(routes::authentication::reset::build_template(request))
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/auth/reset) => {
|
|
routes::authentication::reset::handle_form(request)
|
|
},
|
|
|
|
(GET) (/guide) => {
|
|
Response::html(routes::guide::build_template())
|
|
},
|
|
|
|
(GET) (/settings) => {
|
|
Response::html(routes::settings::menu::build_template())
|
|
},
|
|
|
|
(GET) (/settings/scuttlebutt) => {
|
|
Response::html(routes::settings::scuttlebutt::menu::build_template())
|
|
},
|
|
|
|
(GET) (/settings/scuttlebutt/configure) => {
|
|
Response::html(routes::settings::scuttlebutt::configure::build_template())
|
|
},
|
|
|
|
(GET) (/settings/admin) => {
|
|
Response::html(routes::settings::admin::menu::build_template())
|
|
},
|
|
|
|
(POST) (/settings/admin/add) => {
|
|
routes::settings::admin::add::handle_form(request)
|
|
},
|
|
|
|
(GET) (/settings/admin/configure) => {
|
|
Response::html(routes::settings::admin::configure::build_template(request))
|
|
.reset_flash()
|
|
},
|
|
|
|
(POST) (/settings/admin/delete) => {
|
|
routes::settings::admin::delete::handle_form(request)
|
|
},
|
|
|
|
(GET) (/scuttlebutt/peers) => {
|
|
Response::html(routes::scuttlebutt::peers::build_template())
|
|
},
|
|
|
|
(GET) (/status/scuttlebutt) => {
|
|
Response::html(routes::status::scuttlebutt::build_template())
|
|
},
|
|
|
|
// The code block is called if none of the other blocks matches the request.
|
|
// We return an empty response with a 404 status code.
|
|
_ => Response::empty_404()
|
|
)
|
|
});
|
|
}
|