peach-workspace/peach-web/src/main.rs

131 lines
4.0 KiB
Rust
Raw Normal View History

2021-11-04 13:10:41 +00:00
//! # 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
2022-03-10 09:09:26 +00:00
//! 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;
2021-11-04 13:10:41 +00:00
pub mod error;
2022-03-10 09:09:26 +00:00
//mod router;
//pub mod routes;
//#[cfg(test)]
//mod tests;
mod templates;
2021-11-04 13:10:41 +00:00
pub mod utils;
2021-08-06 17:58:40 +00:00
2022-03-10 09:09:26 +00:00
use std::sync::RwLock;
2021-11-04 13:10:41 +00:00
use lazy_static::lazy_static;
2022-03-10 09:09:26 +00:00
//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};
2021-11-04 21:39:40 +00:00
use utils::Theme;
2021-11-04 13:10:41 +00:00
pub type BoxError = Box<dyn std::error::Error>;
2022-03-10 09:09:26 +00:00
/*
/// 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,
}
2022-03-10 09:09:26 +00:00
*/
lazy_static! {
static ref THEME: RwLock<Theme> = RwLock::new(Theme::Light);
}
2022-03-10 09:09:26 +00:00
//static WLAN_IFACE: &str = "wlan0";
//static AP_IFACE: &str = "ap0";
2022-01-13 13:47:43 +00:00
2022-03-10 09:09:26 +00:00
/*
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>())
2021-11-04 13:10:41 +00:00
}
2022-03-10 09:09:26 +00:00
*/
const HOSTNAME_AND_PORT: &str = "localhost:8000";
2021-11-04 13:10:41 +00:00
2022-03-10 09:09:26 +00:00
/// Launch the peach-web server.
fn main() {
2021-11-04 13:10:41 +00:00
// initialize logger
2021-08-06 17:58:40 +00:00
env_logger::init();
2022-03-10 09:09:26 +00:00
/*
// 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");
}
2022-03-10 09:09:26 +00:00
*/
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(templates::home::build())
},
// 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()
)
});
2021-08-06 17:58:40 +00:00
}