config: function to extract config, using ezidam.toml to store ip address + folder of static assets

This commit is contained in:
Philippe Loctaux 2023-06-29 18:40:45 +02:00
parent 477656a42e
commit b135c49e75
3 changed files with 22 additions and 7 deletions

View file

@ -1,5 +1,7 @@
[default]
address = "0.0.0.0"
template_dir = "./templates"
static_dir = "./static"
[default.databases.ezidam]
url = "../../database/ezidam.sqlite"

View file

@ -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<Build>) -> Rocket<Build> {
use cache::CacheControl;
use database::Database;
@ -46,7 +59,11 @@ pub fn rocket_setup(rocket_builder: Rocket<Build>) -> Rocket<Build> {
}));
// 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);

View file

@ -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)?;