Load values from Rocket.toml into managed state #74
|
@ -32,21 +32,23 @@ pub mod routes;
|
|||
mod tests;
|
||||
pub mod utils;
|
||||
|
||||
use std::{env, process};
|
||||
use std::process;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info};
|
||||
use rocket::{Build, Rocket};
|
||||
use log::{debug, error, info};
|
||||
use rocket::{fairing::AdHoc, serde::Deserialize, Build, Rocket};
|
||||
|
||||
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
|
||||
};
|
||||
/// 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 WLAN_IFACE: &str = "wlan0";
|
||||
|
@ -54,11 +56,27 @@ static AP_IFACE: &str = "ap0";
|
|||
|
||||
pub fn init_rocket() -> Rocket<Build> {
|
||||
info!("Initializing Rocket");
|
||||
if *STANDALONE_MODE {
|
||||
router::mount_peachpub_routes()
|
||||
// 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()
|
||||
}
|
||||
router::mount_peachcloud_routes(rocket)
|
||||
};
|
||||
|
||||
info!("Attaching application configuration to managed state");
|
||||
mounted_rocket.attach(AdHoc::config::<RocketConfig>())
|
||||
}
|
||||
|
||||
/// Launch the peach-web rocket server.
|
||||
|
|
Loading…
Reference in New Issue