Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include default settings in binary, allow overriding settings path #893

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions services/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
56 changes: 34 additions & 22 deletions services/src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,26 +15,44 @@ use url::Url;

static SETTINGS: OnceLock<RwLock<Config>> = 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<Config> {
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<File<_, _>> = 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`
Expand All @@ -46,8 +64,7 @@ fn init_settings() -> RwLock<Config> {
}

/// test may run in subdirectory
#[cfg(test)]
fn retrieve_settings_dir() -> Result<PathBuf> {
pub fn retrieve_settings_dir() -> Result<PathBuf> {
use crate::error::Error;

const MAX_PARENT_DIRS: usize = 1;
Expand All @@ -68,11 +85,6 @@ fn retrieve_settings_dir() -> Result<PathBuf> {
Err(Error::MissingSettingsDirectory)
}

#[cfg(not(test))]
fn retrieve_settings_dir() -> Result<PathBuf> {
std::env::current_dir().context(error::MissingWorkingDirectory)
}

#[cfg(test)]
pub fn set_config<T>(key: &str, value: T) -> Result<()>
where
Expand Down
11 changes: 9 additions & 2 deletions services/tests/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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}");

Expand Down
Loading