diff --git a/README.md b/README.md index 10e28c3e8..6adb4770f 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ The PostgreSQL storage backend can optionally be enabled using `--features postg ### Configuration Copy `Settings-default.toml` to `Settings.toml` and edit per your requirements. +You can override the location of the settings file by using the `GEOENGINE_SETTINGS_FILE_PATH` environment variable. ## Troubleshooting diff --git a/services/src/bin/main.rs b/services/src/bin/main.rs index d43c91184..844290e1e 100644 --- a/services/src/bin/main.rs +++ b/services/src/bin/main.rs @@ -14,7 +14,11 @@ use tracing_subscriber::EnvFilter; use tracing_subscriber::Layer; #[tokio::main] +#[allow(clippy::dbg_macro)] +#[allow(unused_must_use)] + async fn main() { + dbg!(std::env::var("GEOENGINE_SETTINGS_FILE_PATH")); start_server().await.unwrap(); } diff --git a/services/src/util/config.rs b/services/src/util/config.rs index bc986fa4d..a9b8e683b 100644 --- a/services/src/util/config.rs +++ b/services/src/util/config.rs @@ -2,7 +2,7 @@ use crate::contexts::SessionId; use crate::datasets::upload::VolumeName; use crate::error::{self, Result}; use crate::util::parsing::{deserialize_api_prefix, deserialize_base_url_option}; -use config::{Config, Environment, File}; +use config::{Config, Environment, File, FileFormat}; use geoengine_datatypes::primitives::TimeInterval; use geoengine_operators::util::raster_stream_to_geotiff::GdalCompressionNumThreads; use serde::Deserialize; @@ -15,26 +15,44 @@ use url::Url; static SETTINGS: OnceLock> = OnceLock::new(); +const SETTINGS_FILE_PATH_OVERRIDE_ENV_VAR: &str = "GEOENGINE_SETTINGS_FILE_PATH"; + // TODO: change to `LazyLock' once stable +#[allow(clippy::dbg_macro)] fn init_settings() -> RwLock { let mut settings = Config::builder(); let dir: PathBuf = retrieve_settings_dir().expect("settings directory should exist"); - #[cfg(test)] - let files = ["Settings-default.toml", "Settings-test.toml"]; - - #[cfg(not(test))] - let files = ["Settings-default.toml", "Settings.toml"]; - - let files: Vec> = files - .iter() - .map(|f| dir.join(f)) - .filter(|p| p.exists()) - .map(File::from) - .collect(); - - settings = settings.add_source(files); + // include the default settings in the binary + let default_settings = include_str!("../../../Settings-default.toml"); + + settings = settings.add_source(File::from_str(default_settings, FileFormat::Toml)); + + std::env::vars().for_each(|(key, value)| { + dbg!((&key, &value)); + }); + + if let Ok(settings_file_path) = std::env::var(SETTINGS_FILE_PATH_OVERRIDE_ENV_VAR) { + dbg!( + "Settings file path: {} (set by environment variable)", + &settings_file_path + ); + // override the settings file path + settings = settings.add_source(File::with_name(&settings_file_path)); + } else { + // use the default settings file path + #[cfg(test)] + { + dbg!("Settings file path: Settings-test.toml"); + settings = settings.add_source(File::from(dir.join("Settings-test.toml"))); + } + #[cfg(not(test))] + { + dbg!("Settings file path: Settings.toml"); + settings = settings.add_source(File::from(dir.join("Settings.toml"))); + } + } // Override config with environment variables that start with `GEOENGINE_`, // e.g. `GEOENGINE_WEB__EXTERNAL_ADDRESS=https://path.to.geoengine.io` @@ -46,8 +64,7 @@ fn init_settings() -> RwLock { } /// test may run in subdirectory -#[cfg(test)] -fn retrieve_settings_dir() -> Result { +pub fn retrieve_settings_dir() -> Result { use crate::error::Error; const MAX_PARENT_DIRS: usize = 1; @@ -68,11 +85,6 @@ fn retrieve_settings_dir() -> Result { Err(Error::MissingSettingsDirectory) } -#[cfg(not(test))] -fn retrieve_settings_dir() -> Result { - std::env::current_dir().context(error::MissingWorkingDirectory) -} - #[cfg(test)] pub fn set_config(key: &str, value: T) -> Result<()> where diff --git a/services/tests/startup.rs b/services/tests/startup.rs index 103256ad4..5f3d13d36 100644 --- a/services/tests/startup.rs +++ b/services/tests/startup.rs @@ -17,10 +17,17 @@ impl Drop for DroppingServer { } impl DroppingServer { + #[allow(clippy::dbg_macro)] fn new(schema_name: &str) -> Self { + let dir = geoengine_services::util::config::retrieve_settings_dir().unwrap(); + dbg!("Settings dir: {:?}", &dir); + let process = Command::cargo_bin("main") .unwrap() - .env("GEOENGINE_WEB__BACKEND", "postgres") + .env( + "GEOENGINE_SETTINGS_FILE_PATH", + dir.join("Settings-test.toml"), + ) .env("GEOENGINE_POSTGRES__SCHEMA", schema_name) .stderr(Stdio::piped()) .spawn() @@ -57,7 +64,7 @@ async fn it_starts_without_warnings_and_accepts_connections() { // read log output and check for warnings let mut startup_succesful = false; for line in server.stderr_lines().take(100) { - // eprintln!("Line: {line}"); + eprintln!("Line: {line}"); assert!(!line.contains("WARN"), "Warning in log output: {line}");