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

80 lines
2.4 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
//! interacting with the device. The stack currently consists of Rocket (Rust
//! web framework), Tera (Rust template engine inspired by Jinja2 and the Django
//! template language), HTML, CSS and JavaScript. Additional functionality is
//! provided by JSON-RPC clients for the `peach-network` and `peach-stats`
//! microservices.
//!
//! HTML is rendered server-side. Request handlers call JSON-RPC microservices
//! and serve HTML and assets. A JSON API is exposed for remote calls and
//! dynamic client-side content updates via vanilla JavaScript following
2021-11-04 21:30:29 +00:00
//! unobstructive design principles. Each Tera template is passed a context
2021-11-04 13:10:41 +00:00
//! object. In the case of Rust, this object is a `struct` and must implement
//! `Serialize`. The fields of the context object are available in the context
//! of the template to be rendered.
2021-08-06 17:58:40 +00:00
2021-11-04 13:10:41 +00:00
#![feature(proc_macro_hygiene, decl_macro)]
2021-08-06 17:58:40 +00:00
2022-01-13 13:47:43 +00:00
mod context;
2021-11-04 13:10:41 +00:00
pub mod error;
mod router;
2021-11-04 13:10:41 +00:00
pub mod routes;
#[cfg(test)]
mod tests;
pub mod utils;
2021-08-06 17:58:40 +00:00
use std::{env, process};
2021-11-04 13:10:41 +00:00
use lazy_static::lazy_static;
use log::{error, info};
use rocket::{Build, Rocket};
2021-11-04 21:39:40 +00:00
2021-11-04 13:10:41 +00:00
pub type BoxError = Box<dyn std::error::Error>;
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
};
}
2022-01-13 13:47:43 +00:00
static WLAN_IFACE: &str = "wlan0";
static AP_IFACE: &str = "ap0";
pub fn init_rocket() -> Rocket<Build> {
info!("Initializing Rocket");
if *STANDALONE_MODE {
2022-01-18 10:50:06 +00:00
router::mount_peachpub_routes()
} else {
2022-01-18 10:50:06 +00:00
router::mount_peachcloud_routes()
}
2021-11-04 13:10:41 +00:00
}
/// Launch the peach-web rocket server.
#[rocket::main]
async fn main() {
// initialize logger
2021-08-06 17:58:40 +00:00
env_logger::init();
2021-11-04 21:30:29 +00:00
// initialize rocket
2021-11-04 13:10:41 +00:00
let rocket = init_rocket();
2021-11-04 21:30:29 +00:00
// launch rocket
2021-11-04 13:10:41 +00:00
info!("Launching Rocket");
2021-11-15 09:59:25 +00:00
if let Err(e) = rocket.launch().await {
2021-11-04 21:39:40 +00:00
error!("Error in Rocket application: {}", e);
process::exit(1);
}
2021-08-06 17:58:40 +00:00
}