From b135c49e756619e76073f4ee8346dcf966023d21 Mon Sep 17 00:00:00 2001 From: Philippe Loctaux Date: Thu, 29 Jun 2023 18:40:45 +0200 Subject: [PATCH] config: function to extract config, using ezidam.toml to store ip address + folder of static assets --- crates/ezidam/{Rocket.toml => ezidam.toml} | 2 ++ crates/ezidam/src/lib.rs | 19 ++++++++++++++++++- crates/ezidam/src/main.rs | 8 ++------ 3 files changed, 22 insertions(+), 7 deletions(-) rename crates/ezidam/{Rocket.toml => ezidam.toml} (82%) diff --git a/crates/ezidam/Rocket.toml b/crates/ezidam/ezidam.toml similarity index 82% rename from crates/ezidam/Rocket.toml rename to crates/ezidam/ezidam.toml index 0713819..82a0d24 100644 --- a/crates/ezidam/Rocket.toml +++ b/crates/ezidam/ezidam.toml @@ -1,5 +1,7 @@ [default] +address = "0.0.0.0" template_dir = "./templates" +static_dir = "./static" [default.databases.ezidam] url = "../../database/ezidam.sqlite" diff --git a/crates/ezidam/src/lib.rs b/crates/ezidam/src/lib.rs index 215c032..7cb06f3 100644 --- a/crates/ezidam/src/lib.rs +++ b/crates/ezidam/src/lib.rs @@ -21,6 +21,19 @@ mod tokens; pub use crate::email::init as email_init; use crate::minify::Minify; +pub fn config() -> rocket::figment::Figment { + use rocket::figment::providers::*; + use rocket::figment::Figment; + use rocket::Config; + + // rocket defaults + Figment::from(Config::default()) + // from `ezidam.toml` + .merge(Toml::file(Env::var_or("EZIDAM_CONFIG", "ezidam.toml")).nested()) + // from code below + .merge(("ip_header", "x-forwarded-for")) +} + pub fn rocket_setup(rocket_builder: Rocket) -> Rocket { use cache::CacheControl; use database::Database; @@ -46,7 +59,11 @@ pub fn rocket_setup(rocket_builder: Rocket) -> Rocket { })); // Static assets - let rocket_builder = rocket_builder.mount("/", FileServer::from("static")); + let static_assets: String = rocket_builder + .figment() + .extract_inner("static_dir") + .unwrap_or("static".into()); + let rocket_builder = rocket_builder.mount("/", FileServer::from(static_assets)); // Minify content let rocket_builder = Minify::rocket(rocket_builder); diff --git a/crates/ezidam/src/main.rs b/crates/ezidam/src/main.rs index fedb2f2..1464895 100644 --- a/crates/ezidam/src/main.rs +++ b/crates/ezidam/src/main.rs @@ -4,12 +4,8 @@ use rocket::main; // see for using rocket with main function https://github.com/intellij-rust/intellij-rust/issues/5975#issuecomment-920620289 #[main] async fn main() -> Result<(), String> { - // Custom config - // - rocket defaults - // - from `Rocket.toml` - // - from env variables - // - from code below - let config = rocket::Config::figment().merge(("ip_header", "x-forwarded-for")); + // Get config + let config = ezidam::config(); // Get email config let email_config = ezidam::email_init(&config)?;